Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return truly empty body in rails? i.e. content-length 0

I'm trying to return a 200 status with an empty body but rails returns a body with a single space. i.e. content-length 1

For instance, this code generates a body with the single space

respond_to do |f|
  f.html {head :ok}
end

and so does this

respond_to do |f|
  f.html {render :nothing => true}
end

Yes, even render :nothing generates something.

All of this seems to be flowing from a 2005 patch in rails that was designed to fix a bug in Safari where it would ignore the headers if the body was empty. (http://dev.rubyonrails.org/changeset/1818)

Does anyone have any thoughts on how to get a 200 status but with a truly empty body? Background: I'm using an API that makes calls to my controllers. I need to send a 200 but the single space body causes the API to malfunction (parse error...). Also, I'll be deploying to Heroku so I can't patch ActionPack to undo the 2005 hack.

Thanks for any thoughts.

like image 945
rhh Avatar asked Jul 28 '10 08:07

rhh


1 Answers

This appears to work

render :text => ""

For Rails 3, you could use:

render :nothing => true

In Rails 5, render :nothing => true is deprecated (planned for removal in 5.1)

Use

def action
  head :ok
end

Credit to this question for the Rails 5 solution.

like image 52
ngoozeff Avatar answered Oct 31 '22 17:10

ngoozeff