Haskell使用POST REQUEST中的数据



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

这是我的代码:

{-# 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.

如何解决这种类型的错误?

看起来您被do表示法和一元/非一元代码弄糊涂了。以下是我的写作方式。

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

相关内容

  • 没有找到相关文章

最新更新