Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Automatically parse posted JSON content for Mime::JSON requests. [rick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9242 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 4d594cffcfc93b37fad4e423ec8593299e50133c 1 parent 0ff7a2d
@technoweenie technoweenie authored
View
10 actionpack/CHANGELOG
@@ -1,5 +1,15 @@
*SVN*
+* Automatically parse posted JSON content for Mime::JSON requests. [rick]
+
+ POST /posts
+ {"post": {"title": "Breaking News"}}
+
+ def create
+ @post = Post.create params[:post]
+ # ...
+ end
+
* add json_escape ERB util to escape html entities in json strings that are output in HTML pages. [rick]
* Provide a helper proxy to access helper methods from outside views. Closes #10839 [Josh Peek]
View
5 actionpack/lib/action_controller/base.rb
@@ -316,9 +316,10 @@ class Base
# A YAML parser is also available and can be turned on with:
#
# ActionController::Base.param_parsers[Mime::YAML] = :yaml
- @@param_parsers = { Mime::MULTIPART_FORM => :multipart_form,
+ @@param_parsers = { Mime::MULTIPART_FORM => :multipart_form,
Mime::URL_ENCODED_FORM => :url_encoded_form,
- Mime::XML => :xml_simple }
+ Mime::XML => :xml_simple,
+ Mime::JSON => :json }
cattr_accessor :param_parsers
# Controls the default charset for all renders.
View
15 actionpack/lib/action_controller/request.rb
@@ -402,6 +402,14 @@ def parse_formatted_request_parameters
body.blank? ? {} : Hash.from_xml(body).with_indifferent_access
when :yaml
YAML.load(body)
+ when :json
+ if body.blank?
+ {}
+ else
+ data = ActiveSupport::JSON.decode(body)
+ data = {:_json => data} unless data.is_a?(Hash)
+ data.with_indifferent_access
+ end
else
{}
end
@@ -507,7 +515,6 @@ def get_typed_value(value)
end
end
-
MULTIPART_BOUNDARY = %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n
EOL = "\015\012"
@@ -604,12 +611,12 @@ def read_multipart(body, boundary, content_length, env)
end
raise EOFError, "bad boundary end of body part" unless boundary_end=~/--/
- begin
+ begin
body.rewind if body.respond_to?(:rewind)
- rescue Errno::ESPIPE
+ rescue Errno::ESPIPE
# Handles exceptions raised by input streams that cannot be rewound
# such as when using plain CGI under Apache
- end
+ end
params
end
View
2  actionpack/lib/action_view/template_handlers/erb.rb
@@ -3,7 +3,7 @@
class ERB
module Util
HTML_ESCAPE = { '&' => '&amp;', '>' => '&gt;', '<' => '&lt;', '"' => '&quot;' }
- JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003E', '<' => '\u003C'}
+ JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003E', '<' => '\u003C' }
# A utility method for escaping HTML tag characters.
# This method is also aliased as <tt>h</tt>.
View
23 actionpack/test/controller/request_test.rb
@@ -851,8 +851,13 @@ def process(name)
end
end
-
class XmlParamsParsingTest < Test::Unit::TestCase
+ def test_hash_params
+ person = parse_body("<person><name>David</name></person>")[:person]
+ assert_kind_of Hash, person
+ assert_equal 'David', person['name']
+ end
+
def test_single_file
person = parse_body("<person><name>David</name><avatar type='file' name='me.jpg' content_type='image/jpg'>#{ActiveSupport::Base64.encode64('ABC')}</avatar></person>")
@@ -899,3 +904,19 @@ def parse_body(body)
ActionController::CgiRequest.new(cgi).request_parameters
end
end
+
+class JsonParamsParsingTest < Test::Unit::TestCase
+ def test_hash_params
+ person = parse_body({:person => {:name => "David"}}.to_json)[:person]
+ assert_kind_of Hash, person
+ assert_equal 'David', person['name']
+ end
+
+ private
+ def parse_body(body)
+ env = { 'CONTENT_TYPE' => 'application/json',
+ 'CONTENT_LENGTH' => body.size.to_s }
+ cgi = ActionController::Integration::Session::StubCGI.new(env, body)
+ ActionController::CgiRequest.new(cgi).request_parameters
+ end
+end

4 comments on commit 4d594cf

@eggie5

been waiting for someone to implement this for a long time!

@TipMobileMirko

Excellent, just what we need! Thanks. :)

@monde

Everybody gets Post-Receive hook support for free now http://github.com/guides/post-receive-hooks

@topfunky

This will be useful.

Please sign in to comment.
Something went wrong with that request. Please try again.