Skip to content

Commit

Permalink
Fix #417: Rails 4 does not rewind input, causes POSTed data to be empty.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Jun 4, 2013
1 parent 948696f commit 37fe09a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Next Release
* [#412](https://github.com/intridea/grape/issues/412): Fix: specifying `content_type` will also override the selection of the data formatter - [@dblock](https://github.com/dblock).
* [#383](https://github.com/intridea/grape/issues/383): Fix: Mounted APIs aren't inheriting settings (including `before` and `after` filters) - [@seanmoon](https://github.com/seanmoon).
* [#408](https://github.com/intridea/grape/pull/408): Fix: Goliath passes request header keys as symbols not strings - [@bobek](https://github.com/bobek).
* [#417](https://github.com/intridea/grape/issues/417): Fix: Rails 4 does not rewind input, causes POSTed data to be empty - [@dblock](https://github.com/dblock).
* Your contribution here.

0.4.1 (4/1/2013)
Expand Down
7 changes: 4 additions & 3 deletions lib/grape/middleware/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ def read_body_input
(! request.parseable_data?) &&
(request.content_length.to_i > 0 || request.env['HTTP_TRANSFER_ENCODING'] == 'chunked')

if env['rack.input'] && (body = (env['api.request.input'] = env['rack.input'].read)).length > 0
read_rack_input body
end
input = env['rack.input']
input.rewind if input
body = env['api.request.input'] = input.read if input
read_rack_input(body) if body && body.length > 0
end
end

Expand Down
13 changes: 13 additions & 0 deletions spec/grape/middleware/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,19 @@ 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 "rewinds IO" do
io = StringIO.new('{"is_boolean":true,"string":"thing"}')
io.read
subject.call({
'PATH_INFO' => '/infol',
'REQUEST_METHOD' => method,
'CONTENT_TYPE' => 'application/json',
'rack.input' => io,
'HTTP_TRANSFER_ENCODING' => 'chunked'
})
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 an xml #{method} and copies values into rack.request.from_hash' do
io = StringIO.new('<thing><name>Test</name></thing>')
subject.call({
Expand Down

0 comments on commit 37fe09a

Please sign in to comment.