0

我想通过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.

如何解决这种类型错误?

4

1 回答 1

1

看起来您对 do-notation 和单子/非单子代码感到困惑。这就是我的写法。

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
于 2015-12-06T22:47:45.223 回答