开发者

Handling POST using Warp/WAI

开发者 https://www.devze.com 2023-04-05 03:02 出处:网络
How do you retrieve data from a POST request using Network.Wai and Warp? Say for example, I have a simple webpage

How do you retrieve data from a POST request using Network.Wai and Warp?

Say for example, I have a simple webpage

....
<form method="POST" action="/handlepost">
    <input name="name" type开发者_如何学编程="text" />
    <input type="submit" />
</form>
....

When the user clicks submit, how can I retrieve this data? I know how to get GET data (queryString)

for example

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?


Just to add to hammar's answer: the wai package itself just defines the interface, it doesn't provide any helper functions. What you're looking for is the wai-extra package, in particular parseRequestBody. Note that this allows you to control exactly how the uploaded files are stored, such as in temporary files or in memory.


WAI is quite a low level interface, so POST data is left unprocessed in the request body, just as it was received. You should be able to grab it using the requestBody function.

Of course, you will then have to parse it, as it's typically encoded in the application/x-www-form-urlencoded format (or multipart/form-data for a form with file upload). I suspect there might be helper functions for this somewhere, but I could not find any in the WAI package itself, at least.

0

精彩评论

暂无评论...
验证码 换一张
取 消