httphaskellpostweb-crawlerhttp-conduit

Haskell use Data from POST REQUEST


I want to get the body of geocodeip.com with a POST request (the ip in the textbox).

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?

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?


Solution

  • It looks like you are confused by do-notation and monadic/non-monadic code. 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