[Haskell-beginners] Network.HTTP.Conduit - body in REST/POST request

Michael Snoyman michael at snoyman.com
Wed May 28 16:18:31 UTC 2014


Just to clarify one point: you don't need to include the ?, :, or &
characters in the parameters to urlEncodedBody; urlEncodedBody will
automatically add the ? and &, and I'm guessing the colon was just a typo.
In other words, the following code is probably closer to what you were
looking for:

let request = urlEncodedBody [("nonce", "2"), ("method", "getInfo")] r


On Wed, May 28, 2014 at 4:21 PM, Miro Karpis <miroslav.karpis at gmail.com>wrote:

> so this did the trick:
>
> test :: IO ()
> test = do
>   r <- parseUrl $ "http://localhost:3000/readbody"
>    let r_ = urlEncodedBody [("?nonce:", "2"), ("&method", "getInfo")] r
>   let request = r_
>         { requestBody = (RequestBodyBS s)}
>
>   response <- withManager $ httpLbs request
>   putStrLn $ show request
>   print $ responseBody response
>
>
> On Wed, May 28, 2014 at 2:48 PM, Miro Karpis <miroslav.karpis at gmail.com>wrote:
>
>> Hi, I'm having difficulties with sending body parameters/values in a POST
>> request. It seems that the server does not receive in body.
>>
>> ideas/comments very welcome ;-)
>>
>> cheers,
>> m.
>>
>>
>> {-# LANGUAGE OverloadedStrings #-}
>>
>> import Network.HTTP.Conduit
>> import Data.Word(Word8)
>> import Data.ByteString.Lazy(pack)
>> import qualified Data.ByteString.Char8 as B
>>
>> x :: B.ByteString
>> x = "?value=10"
>>
>> post = do
>> r <- parseUrl "http://postcatcher.in/catchers/5385d4e0b6887c0200000071"
>>  putStrLn $show r
>> let request = r
>> { secure = True
>>  , method = "POST"
>> , requestBody = RequestBodyBS s
>> , requestHeaders = (requestHeaders r) ++ [("Content-Type",
>> "application/json")]}
>>  putStrLn $ show request
>> response <- withManager $ httpLbs request
>> print $ responseBody response
>>
>>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140528/d535f235/attachment.html>


More information about the Beginners mailing list