Haskell使用POST REQUEST中的数据

时间:2015-12-06 21:36:03

标签: http haskell post web-crawler http-conduit

我想通过POST请求获取geocodeip.com的正文(文本框中的ip)。

这是我的代码:

{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE OverloadedStrings #-}

module Main where

import Foreign.C.Types
import Foreign.C.String

import Network.HTTP.Conduit
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Char8 as C8
import Text.HTML.TagSoup
getGPS :: String -> IO ()
getGPS ip = do
  initReq <- parseUrl "http://www.geocodeip.com/"
  let req = (flip urlEncodedBody) initReq $ [("IP", C8.pack ip)]
  let res = withManager $ httpLbs req
  tags <- fmap parseTags ( (responseBody res))
  print tags

--foreign export ccall getGPS :: CString -> IO ()

到目前为止,如果我“完成”他与L.putStr $ responseBody res一起工作,那么它是有效的...但是我怎样才能得到tags

编译错误:

    Couldn't match type ‘Response L.ByteString’ with ‘L.ByteString’
    Expected type: Response L.ByteString
      Actual type: Response (Response L.ByteString)
    In the first argument of ‘responseBody’, namely ‘res’
    In the second argument of ‘($)’, namely ‘responseBody res’
Failed, modules loaded: none.

如何解决此类错误?

1 个答案:

答案 0 :(得分:1)

看起来你会被do-notation和monadic / non-monadic代码弄糊涂。 我就是这样写的。

getGPS :: String -> IO ()
getGPS ip = do
  initReq <- parseUrl "http://www.geocodeip.com/"
  let req = urlEncodedBody [("IP", C8.pack ip)] initReq
  res <- withManager $ httpLbs req
  let tags = parseTags (responseBody res)
  print tags