diff options
-rw-r--r-- | lib/rack/request.rb | 10 | ||||
-rw-r--r-- | test/spec_request.rb | 10 |
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("/", |