HaskellでHTTPリクエスト送信(2)・HTTPレスポンス取得
HTTPリクエストを送信する方法を前の記事で書き忘れたけど、Request型の値をそのまま送信するには
simpleHTTP :: HStream ty => Request ty -> IO (Result (Response ty))
を使えばいいようです。当然リクエスト送信なので返り値はIO型。
ty型はとりあえずStringやByteStringが使えて、返り値の型はこんな感じ。
type Result a = Either ConnError a data Response a = Response { rspCode :: ResponseCode , rspReason :: String , rspHeaders :: [Header] , rspBody :: a } type ResponseCode = (Int, Int, Int)
ステータスが
・"302 Found " なら rspCode = (3, 0, 2), rspReason = "Found "
・"200 OK " なら rspCode = (2, 0, 0), rspReason = "OK "
が入ります。
rspHeadersにはRequest型と同じく各種ヘッダに対応するHeader型が、rspBodyにはResponse Bodyが入ります。
・Output
rspCode: (2,0,0) rspReason: "OK " rspHeaders: [Date: Sun, 18 Mar 2012 14:23:27 GMT ,Server: Apache/2.2.9 (Debian) mod_python/3.3.1 Python/2.5.2 ,Last-Modified: Wed, 22 Feb 2012 15:08:10 GMT ,ETag: "1886002-aba-4b98ee5476a80" ,Accept-Ranges: bytes ,Content-Length: 2746 ,Vary: Accept-Encoding ,Connection: close ,Content-Type: text/html ] rspBody: "<!DOCTYPE HTML ......