Hi, Please can someone explain how,using the wreq package, I can download and save a binary file? Say the file is at the end of http://x.y.z/files/myfile.jpg and it is a jpeg and no authentication is needed. I just want to 1. Check that the URL is syntactically correct - flag and error if not 2. If the URL is syntactically ok then download the file using GET. 3. Check that the response code is 200 and if so save the file 3a. if the response code is not 200 then delegate to an error handling function or some simple idiomatic way of error handling. Thanks once again. Mike
Hi Mike, First, if you don't know how wreq acts when it gets invalid url – I suggest just launching repl and checking that out. I'll show how to do that using haskell stack tool <https://github.com/commercialhaskell/stack>: ➜ ~ stack install wreq ... ➜ ~ stack ghci Run from outside a project, using implicit global config Using resolver: lts-3.5 from global config file: /Users/kb/.stack/global/stack.yaml Configuring GHCi with the following packages: GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help λ import Network.Wreq λ get "nonvalid" *** Exception: InvalidUrlException "nonvalid" "Invalid URL" You see, it throws InvalidUrlException upon request, which you can catch and render an error. Now, to get content, as per tutorial, just do: λ import Control.Lens λ res <- get "http://i.imgur.com/f0IKpky.png" λ res ^. responseBody ... (long binary response goes into your output) ... That's it. Last thing I should mention – errors which can be thrown are all just HttpException type. Go and see possible ones <http://hackage.haskell.org/package/http-client-0.4.24/docs/Network-HTTP-Client.html#t:HttpException>, some of which you might want to handle specifically, while others just in a generic "something bad happened" error. Hope this helps. On Sun, Oct 4, 2015 at 6:18 PM, Mike Houghton <mike_k_houghton@yahoo.co.uk> wrote:
Hi,
Please can someone explain how,using the wreq package, I can download and save a binary file? Say the file is at the end of http://x.y.z/files/myfile.jpg
and it is a jpeg and no authentication is needed.
I just want to
1. Check that the URL is syntactically correct - flag and error if not 2. If the URL is syntactically ok then download the file using GET. 3. Check that the response code is 200 and if so save the file 3a. if the response code is not 200 then delegate to an error handling function or some simple idiomatic way of error handling.
Thanks once again.
Mike
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Just few more things which might help: λ res ^. responseStatus Status {statusCode = 200, statusMessage = "OK"} λ :t res ^. responseStatus res ^. responseStatus :: Status λ :i Status data Status = Network.HTTP.Types.Status.Status {Network.HTTP.Types.Status.statusCode :: Int, Network.HTTP.Types.Status.statusMessage :: Data.ByteString.Internal.ByteString} -- Defined in ‘Network.HTTP.Types.Status’ instance Enum Status -- Defined in ‘Network.HTTP.Types.Status’ instance Eq Status -- Defined in ‘Network.HTTP.Types.Status’ instance Ord Status -- Defined in ‘Network.HTTP.Types.Status’ instance Show Status -- Defined in ‘Network.HTTP.Types.Status’ You can see how to get the status, where it comes from. So you can just do "if res ^. responseStatus /= status200 then ...". Cheers. On Sun, Oct 4, 2015 at 6:18 PM, Mike Houghton <mike_k_houghton@yahoo.co.uk> wrote:
Hi,
Please can someone explain how,using the wreq package, I can download and save a binary file? Say the file is at the end of http://x.y.z/files/myfile.jpg
and it is a jpeg and no authentication is needed.
I just want to
1. Check that the URL is syntactically correct - flag and error if not 2. If the URL is syntactically ok then download the file using GET. 3. Check that the response code is 200 and if so save the file 3a. if the response code is not 200 then delegate to an error handling function or some simple idiomatic way of error handling.
Thanks once again.
Mike
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Thank you. I’ll work through what you’ve written.
On 5 Oct 2015, at 09:21, Kostiantyn Rybnikov <k-bx@k-bx.com> wrote:
Just few more things which might help:
λ res ^. responseStatus Status {statusCode = 200, statusMessage = "OK"} λ :t res ^. responseStatus res ^. responseStatus :: Status λ :i Status data Status = Network.HTTP.Types.Status.Status {Network.HTTP.Types.Status.statusCode :: Int, Network.HTTP.Types.Status.statusMessage :: Data.ByteString.Internal.ByteString} -- Defined in ‘Network.HTTP.Types.Status’ instance Enum Status -- Defined in ‘Network.HTTP.Types.Status’ instance Eq Status -- Defined in ‘Network.HTTP.Types.Status’ instance Ord Status -- Defined in ‘Network.HTTP.Types.Status’ instance Show Status -- Defined in ‘Network.HTTP.Types.Status’
You can see how to get the status, where it comes from. So you can just do "if res ^. responseStatus /= status200 then ...".
Cheers.
On Sun, Oct 4, 2015 at 6:18 PM, Mike Houghton <mike_k_houghton@yahoo.co.uk <mailto:mike_k_houghton@yahoo.co.uk>> wrote: Hi,
Please can someone explain how,using the wreq package, I can download and save a binary file? Say the file is at the end of http://x.y.z/files/myfile.jpg <http://x.y.z/files/myfile.jpg>
and it is a jpeg and no authentication is needed.
I just want to
1. Check that the URL is syntactically correct - flag and error if not 2. If the URL is syntactically ok then download the file using GET. 3. Check that the response code is 200 and if so save the file 3a. if the response code is not 200 then delegate to an error handling function or some simple idiomatic way of error handling.
Thanks once again.
Mike
_______________________________________________ Beginners mailing list Beginners@haskell.org <mailto:Beginners@haskell.org> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners <http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners>
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (2)
-
Kostiantyn Rybnikov -
Mike Houghton