I've been looking at Network.HTTP, but can't find a way to create properly URL encoded key/value pairs.
How can I generate the post data required from [(key, value)] pair list for example? I imagine something like this already exists (perhaps hidden in the Network.HTTP package) but I can't find it, and I'd rather not re-invent the wheel.
Take a look at urlEncodeVars.
urlEncodeVars :: [(String, String)] -> String
ghci> urlEncodeVars [("language", "Haskell"), ("greeting", "Hello, world!")]
"language=Haskell&greeting=Hello%2C%20world%21"
If you are trying to HTTP POST data x-www-form-urlencoded, urlEncodeVars may not be the right choice. The urlEncodeVars function does not conform to the application/x-www-form-urlencoded encoding algorithm in two ways worth noting:
%20 instead of +
* as %2A instead of *
Note the comment alongside the function in Network.HTTP.Base:
-- Encode form variables, useable in either the
-- query part of a URI, or the body of a POST request.
-- I have no source for this information except experience,
-- this sort of encoding worked fine in CGI programming.
For an example of a conformant encoding, see this function in the hspec-wai package.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With