[Haskell-beginners] Get responsecode(Int) from simpleHTTP's Response
Jacques du Rand
jacquesdr at gmail.com
Wed Oct 17 08:51:00 CEST 2012
This is great works perfectly !
I'm so new at haskell its scary !
One last question i dont *understand* one line ( just the right side )
let (x,y,z) = rspCode response
1) If i look at the documentation: Sorry for HTML. I see the constructors
of Response and below it rspCode,rspReason,rspHeaders etc Are
those parters or functions ?
2) I see the new version has a getResponseCode functions like
getResponseBody with the signature:
getResponseCode :: Result (Response a) -> IO ResponseCode
getResponseCode (Left err) = fail (show err)
getResponseCode (Right r) = return (rspCode r)
What does this mean in the signature *Result (Response a)*
rspReason :: String<http://hackage.haskell.org/packages/archive/base/22.214.171.124/doc/html/Data-Char.html#t:String>
rspHeaders :: [Header<http://hackage.haskell.org/packages/archive/HTTP/4000.0.7/doc/html/Network-HTTP-Headers.html#t:Header>
]rspBody :: a
On Wed, Oct 17, 2012 at 1:32 AM, Michael Orlitzky <michael at orlitzky.com>wrote:
> On 10/16/2012 03:10 PM, Jacques du Rand wrote:
> > HI all
> > I'm trying to write a function that gives me the HTTP code in Int
> > --This is broken
> > getStatusCode::Response->String
> > getStatusCode (Response _,x1,_,_) = x1
> > --this work the download trying to get http status code as well
> > download_file fname url= do
> > let clean_uri = check_url url
> > putStrLn ("Downloading " ++ url ++ "...")
> > rsp <- simpleHTTP (defaultGETRequest_ clean_uri)
> > --problamatic function next line
> > print
> > (getStatusCode rsp)
> > file_buffer <- getResponseBody(rsp)
> > B.writeFile fname file_buffer
> > Best Regards
> There are two reasons this isn't working...
> The first is that simpleHTTP doesn't return a Response object. I'm
> guessing from your variable name that you're expecting one. In fact, it
> returns *either* an error *or* a Response object, so the first thing you
> have to do before you deal with the response is check for an error.
> The second problem is that the response code (within a Response object)
> is not an integer -- it's an ordered pair of three integers (x,y,z). The
> reason stated in the docs is so that it's easy to tell whether or not
> you've got an OK/Error code on your hands.
> This is the simplest thing I could come up with that does what you want.
> module Main
> import Network.HTTP
> main :: IO ()
> main = do
> let req = getRequest "http://michael.orlitzky.com/"
> result <- simpleHTTP req
> case result of
> Left err -> do
> putStrLn "Error!"
> Right response -> do
> let (x,y,z) = rspCode response
> let hundreds = 100*x
> let tens = 10*y
> let ones = z
> let code = hundreds + tens + ones
> putStrLn $ "Response code: " ++ (show code)
> return ()
> You could of course factor out the part that multiplies the (x,y,z) by
> (100,10,1) into a function.
> Beginners mailing list
> Beginners at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners