Cli*_*ton 4 post haskell url-encoding
我一直在寻找Network.HTTP,但无法找到一种方法来创建正确的URL编码键/值对.
例如,如何生成[(key, value)]配对列表中所需的发布数据?我想像这样的东西已经存在(也许隐藏在Network.HTTP包中)但是我找不到它,我宁愿不重新发明轮子.
urlEncodeVars :: [(String, String)] -> String
Run Code Online (Sandbox Code Playgroud)
ghci> urlEncodeVars [("language", "Haskell"), ("greeting", "Hello, world!")]
"language=Haskell&greeting=Hello%2C%20world%21"
Run Code Online (Sandbox Code Playgroud)
如果您尝试 HTTP POST 数据x-www-form-urlencoded,urlEncodeVars可能不是正确的选择。该urlEncodeVars函数不符合application/x-www-form-urlencoded编码算法,有两点值得注意:
%20而不是+*为%2A而不是*请注意中函数旁边的注释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.
Run Code Online (Sandbox Code Playgroud)
对于符合的编码的例子,请参见本功能在hspec-wai包。