Haskell: URL encoding for post data

I watched Network.HTTP , but cannot find a way to correctly create key / value pairs, encoded URLs.

How can I generate the message data required from a list of [(key, value)] pairs, for example? I suppose something like this already exists (perhaps hidden in the Network.HTTP package), but I cannot find it, and I would prefer not to reinvent the wheel.

+4
source share
2 answers

Take a look at urlEncodeVars .

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

If you are trying to execute the HTTP POST data x-www-form-urlencoded , urlEncodeVars may not be the right choice. The urlEncodeVars function urlEncodeVars not match the application / x-www-form-urlencoded coding algorithm in two aspects, which is worth mentioning:

  • it encodes a space like %20 instead of +
  • it encodes * as %2A instead of *

Note the comment next to 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 suitable coding, see this function in the hspec-wai package.

+3
source

All Articles