Skip to content

Commit

Permalink
Invoking body false will return a blank body with 204 No Content.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Dec 16, 2014
1 parent b22da9c commit d15d7c9
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* [#824](https://github.com/intridea/grape/pull/824): Validate array params against list of acceptable values - [@dnd](https://github.com/dnd).
* [#813](https://github.com/intridea/grape/pull/813): Routing methods dsl refactored to get rid of explicit `paths` parameter - [@AlexYankee](https://github.com/AlexYankee).
* [#826](https://github.com/intridea/grape/pull/826): Find `coerce_type` for `Array` when not specified - [@manovotn](https://github.com/manovotn).
* [#645](https://github.com/intridea/grape/issues/645): Invoking `body false` will return `204 No Content` - [@dblock](https://github.com/dblock).
* Your contribution here.

0.9.0 (8/27/2014)
Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
- [Hypermedia](#hypermedia)
- [Rabl](#rabl)
- [Active Model Serializers](#active-model-serializers)
- [Sending Raw or No Data](#sending-raw-data)
- [Authentication](#authentication)
- [Describing and Inspecting an API](#describing-and-inspecting-an-api)
- [Current Route and Endpoint](#current-route-and-endpoint)
Expand Down Expand Up @@ -1886,6 +1887,32 @@ You can use [Active Model Serializers](https://github.com/rails-api/active_model
[grape-active_model_serializers](https://github.com/jrhe/grape-active_model_serializers) gem, which defines a custom Grape AMS
formatter.

## Sending Raw or No Data

In general, use the binary format to send raw data.

```ruby
class API < Grape::API
get '/file' do
content_type 'application/octet-stream'
File.binread 'file.bin'
end
end
```

You can also set the response body explicitly with `body`.

```ruby
class API < Grape::API
get '/' do
content_type 'text/plain'
body 'Hello World'
# return value ignored
end
end
```

Use `body false` to return `204 No Content` without any data or content-type.

## Authentication

Expand Down
3 changes: 3 additions & 0 deletions lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ def cookies
def body(value = nil)
if value
@body = value
elsif value == false
@body = ''
status 204
else
@body
end
Expand Down
29 changes: 29 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2726,4 +2726,33 @@ def before
expect(last_response.body).to eq("{\"error\":\"The requested format 'txt' is not supported.\"}")
end
end

context 'body' do
context 'false' do
before do
subject.get '/blank' do
body false
end
end
it 'returns blank body' do
get '/blank'
expect(last_response.status).to eq(204)
expect(last_response.body).to be_blank
end
end
context 'plain text' do
before do
subject.get '/text' do
content_type 'text/plain'
body 'Hello World'
'ignored'
end
end
it 'returns blank body' do
get '/text'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq 'Hello World'
end
end
end
end
11 changes: 11 additions & 0 deletions spec/grape/dsl/inside_route_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ def initialize
end
end

describe 'false' do
before do
subject.body false
end

it 'sets status to 204' do
expect(subject.body).to eq ''
expect(subject.status).to eq 204
end
end

it 'returns default' do
expect(subject.body).to be nil
end
Expand Down

0 comments on commit d15d7c9

Please sign in to comment.