Skip to content

Commit 76549ed

Browse files
authored
Merge pull request #1550 from jthornec/use-200-for-delete-with-content
Use 200 as default status for deletes that reply with content
2 parents da3e1cc + 9164650 commit 76549ed

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#### Fixes
1010

1111
* [#1548](https://github.com/ruby-grape/grape/pull/1548): Avoid failing even if given path does not match with prefix - [@thomas-peyric](https://github.com/thomas-peyric), [@namusyaka](https://github.com/namusyaka).
12+
* [#1550](https://github.com/ruby-grape/grape/pull/1550): Use 200 as default status for deletes that reply with content - [@jthornec](https://github.com/jthornec).
1213
* Your contribution here.
1314

1415
### 0.19.0 (12/18/2016)

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1764,7 +1764,7 @@ cookies.delete :status_count, path: '/'
17641764

17651765
## HTTP Status Code
17661766

1767-
By default Grape returns a 201 for `POST`-Requests, 204 for `DELETE`-Requests and 200 status code for all other Requests.
1767+
By default Grape returns a 201 for `POST`-Requests, 204 for `DELETE`-Requests that don't return any content, and 200 status code for all other Requests.
17681768
You can use `status` to query and set the actual HTTP Status Code
17691769

17701770
```ruby

UPGRADING.md

+32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
11
Upgrading Grape
22
===============
33

4+
### Upgrading to >= 0.19.1 (next)
5+
6+
#### DELETE now defaults to status code 200 for responses with a body, or 204 otherwise
7+
8+
Prior to this version, DELETE requests defaulted to a status code of 204 No Content, even when the response included content. This behavior confused some clients and prevented the formatter middleware from running properly. As of this version, DELETE requests will only default to a 204 No Content status code if no response body is provided, and will default to 200 OK otherwise.
9+
10+
Specifically, DELETE behaviour has changed as follows:
11+
12+
- In versions < 0.19.0, all DELETE requests defaulted to a 200 OK status code.
13+
- In version 0.19.0, all DELETE requests defaulted to a 204 No Content status code, even when content was included in the response.
14+
- As of version 0.19.1, DELETE requests default to a 204 No Content status code, unless content is supplied, in which case they default to a 200 OK status code.
15+
16+
To achieve the old behavior, one can specify the status code explicitly:
17+
18+
```ruby
19+
delete :id do
20+
status 204 # or 200, for < 0.19.0 behavior
21+
'foo successfully deleted'
22+
end
23+
```
24+
25+
One can also use the new `return_no_content` helper to explicitly return a 204 status code and an empty body for any request type:
26+
27+
```ruby
28+
delete :id do
29+
return_no_content
30+
'this will not be returned'
31+
end
32+
```
33+
34+
See [#1550](https://github.com/ruby-grape/grape/pull/1550) for more information.
35+
436
### Upgrading to >= 0.18.1
537

638
#### Changes in priority of :any routes

lib/grape/dsl/inside_route.rb

+19-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ def status(status = nil)
130130
when Grape::Http::Headers::POST
131131
201
132132
when Grape::Http::Headers::DELETE
133-
204
133+
if @body.present?
134+
200
135+
else
136+
204
137+
end
134138
else
135139
200
136140
end
@@ -181,6 +185,20 @@ def body(value = nil)
181185
end
182186
end
183187

188+
# Allows you to explicitly return no content.
189+
#
190+
# @example
191+
# delete :id do
192+
# return_no_content
193+
# "not returned"
194+
# end
195+
#
196+
# DELETE /12 # => 204 No Content, ""
197+
def return_no_content
198+
status 204
199+
body false
200+
end
201+
184202
# Allows you to define the response as a file-like object.
185203
#
186204
# @example

spec/grape/dsl/inside_route_spec.rb

+15
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ def initialize
111111
expect(subject.status).to eq 204
112112
end
113113

114+
it 'defaults to 200 on DELETE with a body present' do
115+
request = Grape::Request.new(Rack::MockRequest.env_for('/', method: 'DELETE'))
116+
subject.body 'content here'
117+
expect(subject).to receive(:request).and_return(request)
118+
expect(subject.status).to eq 200
119+
end
120+
114121
it 'returns status set' do
115122
subject.status 501
116123
expect(subject.status).to eq 501
@@ -136,6 +143,14 @@ def initialize
136143
end
137144
end
138145

146+
describe '#return_no_content' do
147+
it 'sets the status code and body' do
148+
subject.return_no_content
149+
expect(subject.status).to eq 204
150+
expect(subject.body).to eq ''
151+
end
152+
end
153+
139154
describe '#content_type' do
140155
describe 'set' do
141156
before do

0 commit comments

Comments
 (0)