haskellhaskell-warp

Handling POST using Warp/WAI


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?

Solution

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