Haskell 使用来自 POST REQUEST 的数据

Haskell use Data from 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 ()

到目前为止,如果我 "finish" 他与 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-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