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

Add ability to get request bodies as parameters. Issue #64 #180

Merged
merged 4 commits into from
Jun 4, 2012
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 13 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ along with any named parameters you specify in your route strings.
end
```

Parameters are also populated from the request body on POST and PUT for JSON and XML content-types.

The Request:
```curl -d '{"some_key": "some_value"}' 'http://readercity.com/json_endpoint' -H Content-Type:application/json -v```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit-pick. I am sure readercity is a great site - The rest of examples are with localhost:9292, could you please change this?



The Grape Endpoint:
```ruby
post '/json_endpoint' do
params[:some_key]
end
```

## Headers

Headers are available through the `env` hash object.
Expand Down
21 changes: 20 additions & 1 deletion lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,26 @@ def call!(env)
# The parameters passed into the request as
# well as parsed from URL segments.
def params
@params ||= Hashie::Mash.new.deep_merge(request.params).deep_merge(env['rack.routing_args'] || {})
@params ||= Hashie::Mash.new.
deep_merge(request.params).
deep_merge(env['rack.routing_args'] || {}).
deep_merge(self.body_params)
end

# 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)
return case env['CONTENT_TYPE']
when 'application/json'
MultiJson.decode(request.body.read)
when 'application/xml'
MultiXml.parse(request.body.read)
else
{}
end
end

{}
end

# The API version as specified in the URL.
Expand Down
38 changes: 38 additions & 0 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,45 @@ def app; subject end

get 'rodzyn@test.com/wrong_middle/1'
last_response.status.should == 404
end
end

context 'from body parameters' do
before(:each) do
subject.post '/request_body' do
params[:user]
end

subject.put '/request_body' do
params[:user]
end
end

it 'should convert JSON bodies to params' do
post '/request_body', MultiJson.encode(user: 'Bobby T.'), {'CONTENT_TYPE' => 'application/json'}
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 convert XML bodies to params' do
post '/request_body', '<user>Bobby T.</user>', {'CONTENT_TYPE' => 'application/xml'}
last_response.body.should == 'Bobby T.'
end

it 'should convert XML bodies to params' do
put '/request_body', '<user>Bobby T.</user>', {'CONTENT_TYPE' => 'application/xml'}
last_response.body.should == 'Bobby T.'
end

it 'does not include parameters not defined by the body' do
subject.post '/omitted_params' do
body_params[:version].should == nil
end
post '/omitted_params', MultiJson.encode(user: 'Blah'), {'CONTENT_TYPE' => 'application/json'}
end
end
end
Expand Down