使用Warp/WAI处理POST

djh*_*rld 7 haskell haskell-warp

如何使用Network.Wai和从POST请求中检索数据Warp

比方说,我有一个简单的网页

....
<form method="POST" action="/handlepost">
    <input name="name" type="text" />
    <input type="submit" />
</form>
....
Run Code Online (Sandbox Code Playgroud)

当用户单击提交时,如何检索此数据?我知道如何获取GET数据(queryString)

例如

app :: Application
app request = case rawPathInfo request of
                   "/" -> return $ displayForm
                   "/handlePost" -> return $ handlepost
                   _ -> return $ notFound

displayForm :: Response
displayForm = ResponseBuilder
    status200
    [("Content-Type", "text/html")] $
    fromByteString "<form method='POST' action='/handlepost'><input name="name" type="text" /><input type='submit'></form>"

handlePost :: Request -> Response
handlePost req = undefined -- how do I examine the contents of POST?
Run Code Online (Sandbox Code Playgroud)

Mic*_*man 10

只是添加到hammar的答案:wai包本身只定义了接口,它不提供任何辅助函数.您正在寻找的是wai-extra特别是包装parseRequestBody.请注意,这允许您精确控制上载文件的存储方式,例如临时文件或内存中.


ham*_*mar 7

WAI是一个相当低级别的接口,因此POST数据在请求体中保持未处理状态,就像收到它一样.您应该能够使用该requestBody功能获取它.

当然,您必须解析它,因为它通常以application/x-www-form-urlencoded格式(或multipart/form-data带有文件上载的表单)进行编码.我怀疑这个地方可能有辅助函数,但我至少在WAI包本身找不到任何函数.