简体   繁体   English

Haskell使用POST请求中的数据

[英]Haskell use Data from POST REQUEST

I want to get the body of geocodeip.com with a POST request (the ip in the textbox). 我想通过POST请求(文本框中的ip)获取geocodeip.com的正文。

Here my code: 这是我的代码:

{-# 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 ()

So far it is working if i "finish" he function with L.putStr $ responseBody res ... but how can I get the tags out of this? 到目前为止,如果我“完成”他与L.putStr $ responseBody res一起工作,就可以了,但是如何从中获得tags呢?

Compile-Error: 编译错误:

    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.

How to solve this type-error? 如何解决这种类型错误?

It looks like you are confused by do-notation and monadic/non-monadic code. 看起来您对do-notation和单子/非单子代码感到困惑。 Here is how I would write it. 这是我的写法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM