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 1 commit
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.HTTP.Types (Status, StdMethod)
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)

type ScottyM = ScottyT Text IO
Expand Down Expand Up @@ -177,6 +178,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: 11 additions & 1 deletion 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 @@ -60,6 +61,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 Web.Scotty.Internal.Types
import Web.Scotty.Util

Expand Down Expand Up @@ -176,7 +178,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
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

-- | Get a parameter. First looks in captures, then form data, then query parameters.
--
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
@@ -1,5 +1,6 @@
## next
* Add `MonadThrow` and `MonadCatch` instances for `ActionT` [abhinav]
* Add `formData` [mitchellwrosen]

## 0.11.0
* IO exceptions are no longer automatically turned into ScottyErrors by
Expand Down
1 change: 1 addition & 0 deletions scotty.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Library
data-default-class >= 0.0.1 && < 0.2,
exceptions >= 0.7 && < 0.10,
fail,
http-api-data >= 0.3 && < 0.4,
http-types >= 0.8.2 && < 0.13,
monad-control >= 1.0.0.3 && < 1.1,
mtl >= 2.1.2 && < 2.3,
Expand Down