Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add for decoding x-www-form-urlencoded data #216

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Web/Scotty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Web.Scotty
-- ** Route Patterns
, capture, regex, function, literal
-- ** Accessing the Request, Captures, and Query Parameters
, request, header, headers, body, bodyReader, param, params, jsonData, files
, request, header, headers, body, bodyReader, param, params, jsonData, formData, files
-- ** Modifying the Response and Redirecting
, status, addHeader, setHeader, redirect
-- ** Setting Response Body
Expand Down Expand Up @@ -46,6 +46,7 @@ import Network.Socket (Socket)
import Network.Wai (Application, Middleware, Request, StreamingBody)
import Network.Wai.Handler.Warp (Port)

import Web.FormUrlEncoded (FromForm)
import Web.Scotty.Internal.Types (ScottyT, ActionT, Param, RoutePattern, Options, File, Kilobytes)

type ScottyM = ScottyT Text IO
Expand Down Expand Up @@ -187,6 +188,12 @@ bodyReader = Trans.bodyReader
jsonData :: FromJSON a => ActionM a
jsonData = Trans.jsonData

-- | Parse the request body as @x-www-form-urlencoded@ data and return it. Raises an exception if parse is unsuccessful.
--
-- /Since: FIXME/
formData :: FromForm a => ActionM a
formData = Trans.formData

-- | Get a parameter. First looks in captures, then form data, then query parameters.
--
-- * Raises an exception which can be caught by 'rescue' if parameter is not found.
Expand Down
12 changes: 12 additions & 0 deletions Web/Scotty/Action.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Web.Scotty.Action
, file
, files
, finish
, formData
, header
, headers
, html
Expand Down Expand Up @@ -65,6 +66,7 @@ import Network.Wai

import Numeric.Natural

import qualified Web.FormUrlEncoded as F
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just noticed that CI fails because this module is not in scope. @mitchellwrosen could you add the 'http-api-data' import in the cabal file?

import Prelude ()
import Prelude.Compat

Expand Down Expand Up @@ -198,6 +200,15 @@ bodyReader = ActionT $ getBodyChunk `liftM` ask
jsonData :: (A.FromJSON a, ScottyError e, MonadIO m) => ActionT e m a
jsonData = do
b <- body
either (\e -> raise $ stringError $ "jsonData - no parse: " ++ e ++ ". Data was: " ++ BL.unpack b) return $ A.eitherDecode b

-- | Parse the request body as @x-www-form-urlencoded@ data and return it. Raises an exception if parse is unsuccessful.
--
-- /Since: FIXME/
formData :: (F.FromForm a, ScottyError e, MonadIO m) => ActionT e m a
formData = do
b <- body
either (\e -> raise $ stringError $ "formData - no parse: " ++ ST.unpack e ++ ". Data was: " ++ BL.unpack b) return $ F.urlDecodeAsForm b
when (b == "") $ do
let htmlError = "jsonData - No data was provided."
raiseStatus status400 $ stringError htmlError
Expand All @@ -216,6 +227,7 @@ jsonData = do
A.Success a -> do
return a


-- | Get a parameter. First looks in captures, then form data, then query parameters.
--
-- * Raises an exception which can be caught by 'rescue' if parameter is not found.
Expand Down
2 changes: 1 addition & 1 deletion Web/Scotty/Trans.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module Web.Scotty.Trans
-- ** Route Patterns
, capture, regex, function, literal
-- ** Accessing the Request, Captures, and Query Parameters
, request, header, headers, body, bodyReader, param, params, jsonData, files
, request, header, headers, body, bodyReader, param, params, jsonData, formData, files
-- ** Modifying the Response and Redirecting
, status, addHeader, setHeader, redirect
-- ** Setting Response Body
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

## 0.11.1 [2018.04.07]
* Add `MonadThrow` and `MonadCatch` instances for `ActionT` [abhinav]
* Add `formData` [mitchellwrosen]
* Fix `matchAny` so that all methods are matched, not just standard ones
[taphu]

Expand Down
Loading