简体   繁体   中英

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.

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:

  • 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.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM