2017-01-18 3 views
0

Network.HTTP.Clientを使用していて、ストリーミング応答のヘッダーを読み取ろうとしています。Haskell Network.HTTP.Client withResponse headers

私は型シグネチャがあることをfrom documentationを参照してください。

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

次のように定義される。

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

そして、私は正確に定義しようとしている - のことができるようにする(\ f>のものを行います)応答のすべてのヘッダーを表示するだけです。私は、\はresは理解

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 
    ) 

は実際には次のとおりです。

(Response BodyReader -> IO a) 

私はそれからヘッダを抽出することができますどのように?

responseHeaders :: Response body -> ResponseHeaders 

ghciでこれらのタイプをどのように調査するかについてのヒントもありがとうございます。

+0

素敵なトリックは、機能を考慮に名前と 'undefined'体を与え、GHCiのタイプを確認することです。 ':i'を使用することもできます。 'BodyReader'ま​​たは' ResponseHeaders'が何であるか、どこから来たのかを調べるためのものです。 ( 'responseHeaders'が' body 'を介してレスポンスの内容を受け入れるので、あなたは今のところこれを必要としません) – MarLinn

答えて

2

アクセッサファンクタresponseHeadersを使用できます。あなたは型の署名で正しい直感を持っていました。実際には、searching for that type signature on Stackage's Hoogle yields this function at the first result。元のコードで

、あなたのような何かを探している:

withResponse req man $ print . responseHeaders 
+0

ありがとう、@Michael Snoyman。私はそのパターンから、デバッグで何ができるのかを見ています。 'withResponse req man $(\ x - > do (print。responseStatus)x (print。responseHeaders)x)'などありがとう! – Mittenchops

関連する問題