Skip to content
This repository
Browse code

Responders now return 204 No Content for API requests without a respo…

…nse body (as in the new scaffold)
  • Loading branch information...
commit 80768b739ed7e2053d85a01dd5fa60bde67aad9a 1 parent aef62c4
José Valim authored
2  actionpack/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *Rails 3.2.0 (unreleased)*
2 2
 
  3
+* Responders now return 204 No Content for API requests without a response body (as in the new scaffold) [José Valim]
  4
+
3 5
 * Added ActionDispatch::RequestId middleware that'll make a unique X-Request-Id header available to the response and enables the ActionDispatch::Request#uuid method. This makes it easy to trace requests from end-to-end in the stack and to identify individual requests in mixed logs like Syslog [DHH]
4 6
 
5 7
 * Limit the number of options for select_year to 1000.
22  actionpack/lib/action_controller/metal/responder.rb
@@ -202,10 +202,8 @@ def api_behavior(error)
202 202
         display resource
203 203
       elsif post?
204 204
         display resource, :status => :created, :location => api_location
205  
-      elsif has_empty_resource_definition?
206  
-        display empty_resource, :status => :ok
207 205
       else
208  
-        head :ok
  206
+        head :no_content
209 207
       end
210 208
     end
211 209
 
@@ -269,24 +267,6 @@ def default_action
269 267
       @action ||= ACTIONS_FOR_VERBS[request.request_method_symbol]
270 268
     end
271 269
 
272  
-    # Check whether resource needs a specific definition of empty resource to be valid
273  
-    #
274  
-    def has_empty_resource_definition?
275  
-      respond_to?("empty_#{format}_resource")
276  
-    end
277  
-
278  
-    # Delegate to proper empty resource method
279  
-    #
280  
-    def empty_resource
281  
-      send("empty_#{format}_resource")
282  
-    end
283  
-
284  
-    # Return a valid empty JSON resource
285  
-    #
286  
-    def empty_json_resource
287  
-      "{}"
288  
-    end
289  
-
290 270
     def resource_errors
291 271
       respond_to?("#{format}_resource_errors") ? send("#{format}_resource_errors") : resource.errors
292 272
     end
20  actionpack/test/controller/mime_responds_test.rb
@@ -796,21 +796,21 @@ def test_using_resource_for_put_with_html_rerender_on_failure_even_on_method_ove
796 796
     end
797 797
   end
798 798
 
799  
-  def test_using_resource_for_put_with_xml_yields_ok_on_success
  799
+  def test_using_resource_for_put_with_xml_yields_no_content_on_success
800 800
     @request.accept = "application/xml"
801 801
     put :using_resource
802 802
     assert_equal "application/xml", @response.content_type
803  
-    assert_equal 200, @response.status
  803
+    assert_equal 204, @response.status
804 804
     assert_equal " ", @response.body
805 805
   end
806 806
 
807  
-  def test_using_resource_for_put_with_json_yields_ok_on_success
  807
+  def test_using_resource_for_put_with_json_yields_no_content_on_success
808 808
     Customer.any_instance.stubs(:to_json).returns('{"name": "David"}')
809 809
     @request.accept = "application/json"
810 810
     put :using_resource
811 811
     assert_equal "application/json", @response.content_type
812  
-    assert_equal 200, @response.status
813  
-    assert_equal "{}", @response.body
  812
+    assert_equal 204, @response.status
  813
+    assert_equal " ", @response.body
814 814
   end
815 815
 
816 816
   def test_using_resource_for_put_with_xml_yields_unprocessable_entity_on_failure
@@ -846,23 +846,23 @@ def test_using_resource_for_delete_with_html_redirects_on_success
846 846
     end
847 847
   end
848 848
 
849  
-  def test_using_resource_for_delete_with_xml_yields_ok_on_success
  849
+  def test_using_resource_for_delete_with_xml_yields_no_content_on_success
850 850
     Customer.any_instance.stubs(:destroyed?).returns(true)
851 851
     @request.accept = "application/xml"
852 852
     delete :using_resource
853 853
     assert_equal "application/xml", @response.content_type
854  
-    assert_equal 200, @response.status
  854
+    assert_equal 204, @response.status
855 855
     assert_equal " ", @response.body
856 856
   end
857 857
 
858  
-  def test_using_resource_for_delete_with_json_yields_ok_on_success
  858
+  def test_using_resource_for_delete_with_json_yields_no_content_on_success
859 859
     Customer.any_instance.stubs(:to_json).returns('{"name": "David"}')
860 860
     Customer.any_instance.stubs(:destroyed?).returns(true)
861 861
     @request.accept = "application/json"
862 862
     delete :using_resource
863 863
     assert_equal "application/json", @response.content_type
864  
-    assert_equal 200, @response.status
865  
-    assert_equal "{}", @response.body
  864
+    assert_equal 204, @response.status
  865
+    assert_equal " ", @response.body
866 866
   end
867 867
 
868 868
   def test_using_resource_for_delete_with_html_redirects_on_failure

0 notes on commit 80768b7

A white space is different from an empty body.

HTTP specification is clear about this : "The 204 response MUST NOT include a message-body".

Please sign in to comment.
Something went wrong with that request. Please try again.