18

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.

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
rhh
  • 1,968
  • 3
  • 15
  • 13

1 Answers1

37

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.

David Silva Smith
  • 11,498
  • 11
  • 67
  • 91
ngoozeff
  • 4,576
  • 3
  • 28
  • 21
  • This works for me too! You're awesome! Thanks for the very quick response. I love how your response is super simple too. – rhh Jul 28 '10 at 09:34
  • 2
    `render :nothing => true` returns a content-length of 1 with rails 3.2.13. – YWCA Hello Jun 26 '13 at 18:01
  • Rails 4 answer is here: http://stackoverflow.com/questions/4632271/render-nothing-true-returns-empty-plaintext-file – rmcsharry Jun 02 '16 at 16:33