summary refs log tree commit
diff options
context:
space:
mode:
authorJames Tucker <jftucker@gmail.com>2014-08-03 14:21:56 -0300
committerJames Tucker <jftucker@gmail.com>2014-08-03 14:21:56 -0300
commite4fd87fc24a6bc10598afb83d5f572db96c241c2 (patch)
tree83c1f2f03a3f3b2fe27c6b4bdae23b1871be526b
parentd8e34e9dc9b78ca5fdb2271ef65cac8fd1c4496d (diff)
parent9f52bb1db4b007066c9916881001d15c0a61bf66 (diff)
downloadrack-e4fd87fc24a6bc10598afb83d5f572db96c241c2.tar.gz
Merge branch 'pr/686'
* pr/686:
  add support for application/json content type requests
-rw-r--r--lib/rack/request.rb10
-rw-r--r--test/spec_request.rb10
2 files changed, 18 insertions, 2 deletions
diff --git a/lib/rack/request.rb b/lib/rack/request.rb
index 52ea652c..4f038384 100644
--- a/lib/rack/request.rb
+++ b/lib/rack/request.rb
@@ -148,7 +148,8 @@ module Rack
     # for form-data / param parsing.
     FORM_DATA_MEDIA_TYPES = [
       'application/x-www-form-urlencoded',
-      'multipart/form-data'
+      'multipart/form-data',
+      'application/json'
     ]
 
     # The set of media-types. Requests that do not indicate
@@ -366,7 +367,12 @@ module Rack
       end
 
       def parse_query(qs)
-        Utils.parse_nested_query(qs, '&')
+        case media_type
+        when 'application/json'
+          (qs && qs != '') ? ::Rack::Utils::OkJson.decode(qs) : {}
+        else
+          Utils.parse_nested_query(qs, '&')
+        end
       end
 
       def parse_multipart(env)
diff --git a/test/spec_request.rb b/test/spec_request.rb
index 8a2b4760..c4a7400a 100644
--- a/test/spec_request.rb
+++ b/test/spec_request.rb
@@ -267,6 +267,16 @@ describe Rack::Request do
     input.read.should.equal "foo=bar&quux=bla"
   end
 
+  should "accept application/json content type" do
+    input = StringIO.new('{"foo":"bar","quxx":"bla"}')
+    req = Rack::Request.new \
+      Rack::MockRequest.env_for("/",
+        "CONTENT_TYPE" => 'application/json',
+        :input => input)
+    req.params.should.equal "foo" => "bar", "quxx" => "bla"
+    input.read.should.equal '{"foo":"bar","quxx":"bla"}'
+  end
+
   should "clean up Safari's ajax POST body" do
     req = Rack::Request.new \
       Rack::MockRequest.env_for("/",