Skip to content

Commit b52bf38

Browse files
Don't convert empty body on POST/PUT.
When posting with content type 'application/json' and an empty body, multi_json crashes on the empty string.
1 parent 470c865 commit b52bf38

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

lib/grape/endpoint.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def params
125125

126126
# Pull out request body params if the content type matches and we're on a POST or PUT
127127
def body_params
128-
if ['POST', 'PUT'].include?(request.request_method.to_s.upcase)
128+
if ['POST', 'PUT'].include?(request.request_method.to_s.upcase) && request.body.size > 0
129129
return case env['CONTENT_TYPE']
130130
when 'application/json'
131131
MultiJson.decode(request.body.read)

spec/grape/endpoint_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ def app; subject end
191191
last_response.body.should == 'Bobby T.'
192192
end
193193

194+
it 'should not convert empty JSON bodies to params' do
195+
put '/request_body', '', {'CONTENT_TYPE' => 'application/json'}
196+
last_response.body.should == ''
197+
end
198+
194199
it 'should convert XML bodies to params' do
195200
post '/request_body', '<user>Bobby T.</user>', {'CONTENT_TYPE' => 'application/xml'}
196201
last_response.body.should == 'Bobby T.'

0 commit comments

Comments
 (0)