Mittenchops
Mittenchops

Reputation: 19724

Haskell Network.HTTP.Client withResponse headers

I'm using Network.HTTP.Client and trying to read headers of a streaming response.

I see from documentation that the type signature is:

withResponse :: Request -> Manager -> (Response BodyReader -> IO a) -> IO a 

Defined as:

withResponse req man f = bracket (responseOpen req man) responseClose f

And I'm trying to correctly define (\f -> do stuff) to be able to just show all the headers in the response.

getheaders ::  String -> IO ()
getheaders url = do
  man <- newManager tlsManagerSettings
  req' <- parseUrlThrow url
  putStrLn url
  let req = req' { responseTimeout = Just 1000000}
  withResponse req man $ (\res -> do
    (r _) <- res
    putStrLn $ show r
    )

I understand \res is actually:

(Response BodyReader -> IO a) 

How can I extract the headers from that?

responseHeaders :: Response body -> ResponseHeaders

Any tips also on how you'd investigate these types in ghci also appreciated.

Upvotes: 0

Views: 163

Answers (1)

Michael Snoyman
Michael Snoyman

Reputation: 31355

You can use the responseHeaders accessor functor. You had the right intuition with the type signature. In fact, searching for that type signature on Stackage's Hoogle yields this function at the first result.

With your original code, you're looking for something like:

withResponse req man $ print . responseHeaders

Upvotes: 2

Related Questions