سؤال

I'm trying to get the response code (200,404 etc) from i THINK a Response type in the HTTP class with simpleHTTP Thus far:

--how to get the http response Int from response
getStatusCode response  = print 0

--this works...
--- othercode ---
rsp <- simpleHTTP (defaultGETRequest_ clean_uri) 
file_buffer <- getResponseBody(rsp)
--this fails
response = (getStatusCode rsp)
هل كانت مفيدة؟

المحلول

I think what you want is

getResponseCode :: Result (Response ty) -> IO ResponseCode

from the Network.HTTP module if you're using HTTP-4000.2.4 or later. For earlier versions of HTTP, you would have to pattern-match yourself on the rspCode field apparently, similar to the way shown below for the rspReason field.

If you are interested in the reason, use the rspReason field of Response, after

rsp <- simpleHTTP ...

you have

rsp :: Either ConnError (Response ty)  -- Result is a type synonym for (Either ConnError)

and can access the reason per

let reason = case rsp of
               Left err -> show err  -- for example
               Right response -> rspReason response
putStrLn $ "Here's why: " ++ reason
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top