From 7e41b620ff237b4558d4d19a9dcf9f925ebe9cbe Mon Sep 17 00:00:00 2001 From: Joe Nelson Date: Sun, 15 May 2016 20:45:49 -0700 Subject: [PATCH] Accept POST requests from HTML forms --- CHANGELOG.md | 1 + src/PostgREST/ApiRequest.hs | 4 ++++ test/Feature/InsertSpec.hs | 9 +++++++++ 3 files changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e2a0e972..9a6a53346 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Reload database schema on SIGHUP - @begriffs - Support "-" in column names - @ruslantalpa - Support column/node renaming `alias:column` - @ruslantalpa +- Accept posts from HTML forms - @begriffs ### Fixed - Omit Content-Type header for empty body - @begriffs diff --git a/src/PostgREST/ApiRequest.hs b/src/PostgREST/ApiRequest.hs index 6517e8d05..e32e5c5cc 100644 --- a/src/PostgREST/ApiRequest.hs +++ b/src/PostgREST/ApiRequest.hs @@ -122,6 +122,10 @@ userApiRequest schema req reqBody = Nothing -> PayloadParseError "All lines must have same number of fields" Just json -> PayloadJSON json) (CSV.decodeByName reqBody) + Left "application/x-www-form-urlencoded" -> + PayloadJSON . UniformObjects . V.singleton . M.fromList + . map (cs *** JSON.String . cs) . parseSimpleQuery + $ cs reqBody Left accept -> PayloadParseError $ "Content-type not acceptable: " <> accept diff --git a/test/Feature/InsertSpec.hs b/test/Feature/InsertSpec.hs index 1c86270f1..2a7907b9f 100644 --- a/test/Feature/InsertSpec.hs +++ b/test/Feature/InsertSpec.hs @@ -55,6 +55,15 @@ spec = do , matchHeaders = ["Content-Type" <:> "application/json; charset=utf-8", "Location" <:> "/projects?id=eq.6"] } + context "from an html form" $ + it "accepts disparate json types" $ do + p <- request methodPost "/menagerie" + [("Content-Type", "application/x-www-form-urlencoded")] + ("integer=7&double=2.71828&varchar=forms+are+fun&" <> + "boolean=false&date=1900-01-01&money=$3.99&enum=foo") + liftIO $ do + simpleBody p `shouldBe` "" + simpleStatus p `shouldBe` created201 context "with no pk supplied" $ do context "into a table with auto-incrementing pk" $