Skip to content

Commit d15d7c9

Browse files
committed
Invoking body false will return a blank body with 204 No Content.
1 parent b22da9c commit d15d7c9

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* [#824](https://github.com/intridea/grape/pull/824): Validate array params against list of acceptable values - [@dnd](https://github.com/dnd).
1818
* [#813](https://github.com/intridea/grape/pull/813): Routing methods dsl refactored to get rid of explicit `paths` parameter - [@AlexYankee](https://github.com/AlexYankee).
1919
* [#826](https://github.com/intridea/grape/pull/826): Find `coerce_type` for `Array` when not specified - [@manovotn](https://github.com/manovotn).
20+
* [#645](https://github.com/intridea/grape/issues/645): Invoking `body false` will return `204 No Content` - [@dblock](https://github.com/dblock).
2021
* Your contribution here.
2122

2223
0.9.0 (8/27/2014)

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
- [Hypermedia](#hypermedia)
5555
- [Rabl](#rabl)
5656
- [Active Model Serializers](#active-model-serializers)
57+
- [Sending Raw or No Data](#sending-raw-data)
5758
- [Authentication](#authentication)
5859
- [Describing and Inspecting an API](#describing-and-inspecting-an-api)
5960
- [Current Route and Endpoint](#current-route-and-endpoint)
@@ -1886,6 +1887,32 @@ You can use [Active Model Serializers](https://github.com/rails-api/active_model
18861887
[grape-active_model_serializers](https://github.com/jrhe/grape-active_model_serializers) gem, which defines a custom Grape AMS
18871888
formatter.
18881889

1890+
## Sending Raw or No Data
1891+
1892+
In general, use the binary format to send raw data.
1893+
1894+
```ruby
1895+
class API < Grape::API
1896+
get '/file' do
1897+
content_type 'application/octet-stream'
1898+
File.binread 'file.bin'
1899+
end
1900+
end
1901+
```
1902+
1903+
You can also set the response body explicitly with `body`.
1904+
1905+
```ruby
1906+
class API < Grape::API
1907+
get '/' do
1908+
content_type 'text/plain'
1909+
body 'Hello World'
1910+
# return value ignored
1911+
end
1912+
end
1913+
```
1914+
1915+
Use `body false` to return `204 No Content` without any data or content-type.
18891916

18901917
## Authentication
18911918

lib/grape/dsl/inside_route.rb

+3
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ def cookies
150150
def body(value = nil)
151151
if value
152152
@body = value
153+
elsif value == false
154+
@body = ''
155+
status 204
153156
else
154157
@body
155158
end

spec/grape/api_spec.rb

+29
Original file line numberDiff line numberDiff line change
@@ -2726,4 +2726,33 @@ def before
27262726
expect(last_response.body).to eq("{\"error\":\"The requested format 'txt' is not supported.\"}")
27272727
end
27282728
end
2729+
2730+
context 'body' do
2731+
context 'false' do
2732+
before do
2733+
subject.get '/blank' do
2734+
body false
2735+
end
2736+
end
2737+
it 'returns blank body' do
2738+
get '/blank'
2739+
expect(last_response.status).to eq(204)
2740+
expect(last_response.body).to be_blank
2741+
end
2742+
end
2743+
context 'plain text' do
2744+
before do
2745+
subject.get '/text' do
2746+
content_type 'text/plain'
2747+
body 'Hello World'
2748+
'ignored'
2749+
end
2750+
end
2751+
it 'returns blank body' do
2752+
get '/text'
2753+
expect(last_response.status).to eq(200)
2754+
expect(last_response.body).to eq 'Hello World'
2755+
end
2756+
end
2757+
end
27292758
end

spec/grape/dsl/inside_route_spec.rb

+11
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,17 @@ def initialize
160160
end
161161
end
162162

163+
describe 'false' do
164+
before do
165+
subject.body false
166+
end
167+
168+
it 'sets status to 204' do
169+
expect(subject.body).to eq ''
170+
expect(subject.status).to eq 204
171+
end
172+
end
173+
163174
it 'returns default' do
164175
expect(subject.body).to be nil
165176
end

0 commit comments

Comments
 (0)