Accept empty string as empty json body when calling an rpc (#832)

This commit is contained in:
Marios Koulakis
2017-03-27 21:34:03 -07:00
committed by Joe Nelson
parent 0b486ccf44
commit 9b4b45671c
4 changed files with 24 additions and 7 deletions
+7 -6
View File
@@ -13,6 +13,7 @@ module PostgREST.ApiRequest ( ApiRequest(..)
import Protolude
import qualified Data.Aeson as JSON
import Data.Aeson.Types (emptyObject)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Internal as BS (c2w)
import qualified Data.ByteString.Lazy as BL
@@ -124,13 +125,13 @@ userApiRequest schema req reqBody
payload =
case decodeContentType . fromMaybe "application/json" $ lookupHeader "content-type" of
CTApplicationJSON ->
either Left (\val -> case ensureUniform (pluralize val) of
Nothing -> Left "All object keys must match"
Just json -> Right json) (JSON.eitherDecode reqBody)
note "All object keys must match" . ensureUniform . pluralize
=<< if BL.null reqBody && isTargetingProc
then Right emptyObject
else JSON.eitherDecode reqBody
CTTextCSV ->
either Left (\val -> case ensureUniform (csvToJson val) of
Nothing -> Left "All lines must have same number of fields"
Just json -> Right json) (CSV.decodeByName reqBody)
note "All lines must have same number of fields" . ensureUniform . csvToJson
=<< CSV.decodeByName reqBody
CTOther "application/x-www-form-urlencoded" ->
Right . PayloadJSON . V.singleton . M.fromList
. map (toS *** JSON.String . toS) . parseSimpleQuery