3

My sincere apologies if this question sounds silly or has already been asked; what is the standard way to read in a text file in the same sense that readFile does, except if the file is online?

I am of course given a URL as input, like the following example

https://dotnetperls-controls.googlecode.com/files/enable1.txt

What libraries do I import? Are there good references I can consult for this kind of web-based IO in Haskell?

*Please edit the tags as you see fit.

dxuhuang
  • 990
  • 1
  • 9
  • 18
  • 4
    `simpleHTTP (getRequest "http://www.google.com/robots.txt") :: IO (Either ConnError (Response String))`; or `fmap (fmap rspBody) $ simpleHTTP (getRequest "http://www.google.com/robots.txt")` to just get the body of the response (the contents of the text file). For details: http://hackage.haskell.org/package/HTTP-4000.2.10 – user2407038 Jan 27 '14 at 01:19
  • 1
    You may also use curl package. It is more complicated but very powerful. – user3974391 Jan 27 '14 at 04:47
  • See also [this question](http://stackoverflow.com/q/20017484/1333025). – Petr Jan 27 '14 at 06:41

1 Answers1

4

This is a reproduction of @user2407038's comment, for the sake of having an answer.

import Network.HTTP

main = do
  response <- simpleHTTP $ getRequest "http://www.google.com/robots.txt"
  let body = fmap rspBody response
  print response
  print body
Chris Taylor
  • 46,912
  • 15
  • 110
  • 154