[Haskell-beginners] Network.Curl POST body not showing up

Michael Xavier nemesisdesign at gmail.com
Wed May 11 07:25:56 CEST 2011


Hi all,

I was hoping one of you was pretty proficient with Network.Curl to help me
out, as I am not. I need to post JSON to an HTTPS endpoint using HTTP Basic
Authentication. The endpoint will not accept the request unless the
content-type is set to application/json.

If I were using the curl utility, I'd do something like

curl -H "Content-Type: application/json" -d "{blah:42}" -u 'myuser:mypass'
https://example.com/api.json

I'd really like to use curl's curlGetString because the return type is
perfect, the code and the body.

I've tried curlGetString, curlPost and curlMultiPost and all combinations of
CurlOption, but I can either get it to send the correct content type header
with empty content or send the post body with the content type defaulted to
urlencoded or multipart/form-data. My code right now looks something like:

doPost :: (ToJSON a) => T.Text -> T.Text -> T.Text -> a -> CampfireM
(CurlCode, String)
doPost key sub path pay = liftIO $ curlGetString (url path sub) opts
                     where opts    = method_POST ++ curlOpts key ++
[CurlVerbose True, post, ct]
                           post    = CurlPostFields [T.unpack encPay]
                           ct      = CurlHttpHeaders ["Content-Type:
application/json"]
                           encPath = encodePath path encPay
                           encPay  = encodePayload pay

The post fields will not be in the form of "key=value", but rather a
JSCODE:

* Server auth using Basic with user 'SECRET_KEY'
> POST /rooms/287132/speak.json HTTP/1.1
Authorization: Basic SECRET_KEY
Host: example.com
Accept: */*
Content-Type: application/json
Content-Length: 103

* The requested URL returned error: 500
* Closing connection #0
* HTTP response code said error

It would seem to me from this output that no actual content was sent. What
am I doing wrong? I'd really like to use curlGetString instead of some of
the more complex curl functions.

Thanks!

-- 
Michael Xavier
http://www.michaelxavier.net


-- 
Michael Xavier
http://www.michaelxavier.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110510/349aedec/attachment.htm>


More information about the Beginners mailing list