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

匿名 (未验证) 提交于 2019-12-03 01:49:02

问题:

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.

回答1:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!