如何使用Network.Wai和Warp从POST请求中检索数据
例如,我有一个简单的网页
....
<form method="POST" action="/handlepost">
<input name="name" type="text" />
<input type="submit" />
</form>
....当用户单击提交时,如何检索此数据?我知道如何获取数据(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?发布于 2011-09-15 10:20:52
补充一下hammar的回答: wai包本身只定义了接口,它没有提供任何帮助函数。您要查找的是wai-extra包,特别是parseRequestBody。请注意,这允许您准确地控制上载文件的存储方式,例如存储在临时文件或内存中。
发布于 2011-09-15 05:22:26
WAI是一个相当低级的接口,因此POST数据在请求主体中未被处理,就像它被接收时一样。您应该能够使用requestBody函数获取它。
当然,您必须对其进行解析,因为它通常以application/x-www-form-urlencoded格式编码(对于具有文件上传功能的表单,则为multipart/form-data格式)。我怀疑在某个地方可能有帮助函数,但我至少在WAI包本身中找不到任何帮助函数。
https://stackoverflow.com/questions/7422913
复制相似问题