如何在导轨中返回真正的空体?即内容长度 0 [英] How to return truly empty body in rails? i.e. content-length 0
问题描述
我试图返回 200 状态并带有一个空的主体,但 rails 返回一个带有单个空格的主体.即内容长度 1
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
也是这样
respond_to do |f|
f.html {render :nothing => true}
end
是的,即使是 render :nothing 也会产生一些东西.
Yes, even render :nothing generates something.
所有这些似乎都源自 2005 年的 rails 补丁,该补丁旨在修复 Safari 中的一个错误,如果正文为空,它将忽略标题.(http://dev.rubyonrails.org/changeset/1818)
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)
有没有人对如何获得 200 状态但身体真正空虚有任何想法?背景:我正在使用一个 API 来调用我的控制器.我需要发送 200 但单个空格正文导致 API 出现故障(解析错误...).此外,我将部署到 Heroku,因此我无法修补 ActionPack 来撤消 2005 年的黑客攻击.
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.
感谢您的任何想法.
推荐答案
这似乎有效
render :text => ""
对于 Rails 3,您可以使用:
For Rails 3, you could use:
render :nothing =>真实
在 Rails 5 中,render :nothing =>true
已弃用(计划在 5.1 中移除)
In Rails 5, render :nothing => true
is deprecated (planned for removal in 5.1)
使用
def action
head :ok
end
感谢 Rails 5 解决方案的这个问题.
Credit to this question for the Rails 5 solution.
这篇关于如何在导轨中返回真正的空体?即内容长度 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!