Clinton
Clinton

Reputation: 23135

Haskell: URL encoding for post data

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.

Upvotes: 5

Views: 2509

Answers (3)

Nick Pershyn
Nick Pershyn

Reputation: 59

I recommend trying wreq for this. It provides FormParm data type, so you would want to convert your key-value pairs into [FormParm]. Then you can use something like this:

import qualified Data.ByteString.Char8 as C8
import Network.Wreq (post)
import Network.Wreq.Types (FormParam(..))

myPost = post url values where
  values :: [FormParam]
  values = [C8.pack "key" := ("value" :: String)]
  url = "https://some.domain.name"

Upvotes: 0

Erin Swenson-Healey
Erin Swenson-Healey

Reputation: 138

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:

  • it encodes a space as %20 instead of +
  • it encodes * 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.

Upvotes: 5

icktoofay
icktoofay

Reputation: 129119

Take a look at urlEncodeVars.

urlEncodeVars :: [(String, String)] -> String
ghci> urlEncodeVars [("language", "Haskell"), ("greeting", "Hello, world!")]
"language=Haskell&greeting=Hello%2C%20world%21"

Upvotes: 8

Related Questions