diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index 0537ecb1fd..90d3880bac 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -1,6 +1,7 @@ Next Release ============ +* [#210](/~https://github.com/intridea/grape/pull/210): Fix: `Endpoint#body_params` causing undefined method 'size' - [@adamgotterer](/~https://github.com/adamgotterer). * [#201](/~https://github.com/intridea/grape/pull/201): Rewritten `params` DSL, including support for coercion and validations - [@schmurfy](/~https://github.com/schmurfy). * [#205](/~https://github.com/intridea/grape/pull/205): Fix: Corrected parsing of empty JSON body on POST/PUT - [@tim-vandecasteele](/~https://github.com/tim-vandecasteele). * [#181](/~https://github.com/intridea/grape/pull/181): Fix: Corrected JSON serialization of nested hashes containing `Grape::Entity` instances - [@benrosenblum](/~https://github.com/benrosenblum). diff --git a/lib/grape/endpoint.rb b/lib/grape/endpoint.rb index 063265bd47..eca96166b0 100644 --- a/lib/grape/endpoint.rb +++ b/lib/grape/endpoint.rb @@ -125,7 +125,7 @@ def params # Pull out request body params if the content type matches and we're on a POST or PUT def body_params - if ['POST', 'PUT'].include?(request.request_method.to_s.upcase) && request.body.size > 0 + if ['POST', 'PUT'].include?(request.request_method.to_s.upcase) && request.content_length.to_i > 0 return case env['CONTENT_TYPE'] when 'application/json' MultiJson.decode(request.body.read) diff --git a/spec/grape/endpoint_spec.rb b/spec/grape/endpoint_spec.rb index 2c5d9e61af..d6ff1c5e4e 100644 --- a/spec/grape/endpoint_spec.rb +++ b/spec/grape/endpoint_spec.rb @@ -186,11 +186,6 @@ def app; subject end last_response.body.should == 'Bobby T.' end - it 'should convert JSON bodies to params' do - put '/request_body', MultiJson.encode(:user => 'Bobby T.'), {'CONTENT_TYPE' => 'application/json'} - last_response.body.should == 'Bobby T.' - end - it 'should not convert empty JSON bodies to params' do put '/request_body', '', {'CONTENT_TYPE' => 'application/json'} last_response.body.should == ''