Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request body not included in params when issuing a PATCH request #335

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Next Release
* [#60](https://github.com/intridea/grape/issues/60): Fix: mounting of a Grape API onto a path - [@dblock](http://github.com/dblock).
* [#190](https://github.com/intridea/grape/issues/190): When you add a `GET` route for a resource, a route for the `HEAD` method will also be added automatically. You can disable this behavior with `do_not_route_head!` - [@dblock](http://github.com/dblock).
* Added `do_not_route_options!`, which disables the automatic creation of the `OPTIONS` route - [@dblock](http://github.com/dblock).
* [#335](https://github.com/intridea/grape/pull/335): Fix: Fixed parameters passed in request body when issuing a `PATCH` request not being available in the `params` hash - [@FreakenK](http://github.com/FreakenK).
* Your contribution here.

0.2.6 (01/11/2013)
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/middleware/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def after
private

def read_body_input
if (request.post? || request.put?) && (! request.form_data?) && (! request.parseable_data?) && (request.content_length.to_i > 0)
if (request.post? || request.put? || request.patch?) && (! request.form_data?) && (! request.parseable_data?) && (request.content_length.to_i > 0)
if env['rack.input'] && (body = env['rack.input'].read).length > 0
begin
fmt = mime_types[request.media_type] if request.media_type
Expand Down
26 changes: 25 additions & 1 deletion spec/grape/middleware/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def to_xml

context 'input' do
[ "application/json", "application/json; charset=utf-8" ].each do |content_type|
it 'parses the body from a POST/PUT and put the contents into rack.request.form_hash for #{content_type}' do
it 'parses the body from a POST and put the contents into rack.request.form_hash for #{content_type}' do
io = StringIO.new('{"is_boolean":true,"string":"thing"}')
subject.call({
'PATH_INFO' => '/info',
Expand All @@ -162,6 +162,30 @@ def to_xml
subject.env['rack.request.form_hash']['is_boolean'].should be_true
subject.env['rack.request.form_hash']['string'].should == 'thing'
end
it 'parses the body from a PUT and put the contents into rack.request.form_hash for #{content_type}' do
io = StringIO.new('{"is_boolean":true,"string":"thing"}')
subject.call({
'PATH_INFO' => '/info',
'REQUEST_METHOD' => 'PUT',
'CONTENT_TYPE' => content_type,
'rack.input' => io,
'CONTENT_LENGTH' => io.length
})
subject.env['rack.request.form_hash']['is_boolean'].should be_true
subject.env['rack.request.form_hash']['string'].should == 'thing'
end
it 'parses the body from a PATCH and put the contents into rack.request.form_hash for #{content_type}' do
io = StringIO.new('{"is_boolean":true,"string":"thing"}')
subject.call({
'PATH_INFO' => '/info',
'REQUEST_METHOD' => 'PATCH',
'CONTENT_TYPE' => content_type,
'rack.input' => io,
'CONTENT_LENGTH' => io.length
})
subject.env['rack.request.form_hash']['is_boolean'].should be_true
subject.env['rack.request.form_hash']['string'].should == 'thing'
end
end
it 'parses the body from an xml POST/PUT and put the contents into rack.request.from_hash' do
io = StringIO.new('<thing><name>Test</name></thing>')
Expand Down