HTTP POST,head:ok返回1个神秘字节

时间:2011-08-15 10:55:49

标签: ruby-on-rails http http-headers

我有一个通过移动应用程序访问的Rails应用程序 - 数据以JSON格式交换。

当我执行成功的POST时,我希望并获得一个HTTP代码200 OK。我不期望的是伴随1字节的数据ASCII 0x20(即空格)。

在POST的对象(设备)已经存在的情况下,我有以下代码从POST返回。

    # Device is already registered, so update attributes of existing record (incl. device token)

    if @deviceFound.update_attributes(params[:device])
      format.html { redirect_to(@deviceFound, :notice => 'Device was successfully updated.') }
      format.xml  { head :ok }
      # format.json { head :ok }
      format.json do
        render :nothing => true, :status => :ok
        return true
      end
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @deviceFound.errors, :status => :unprocessable_entity }
      format.json { render :json => @deviceFound.errors, :status => :unprocessable_entity }
    end

从注释掉的行中,你会看到我一直在使用format.json {head:ok}但是为了理解我为什么要返回这个字节,我尝试了另外的实现,我相信是等同的。两者都产生相同的HTTP 200 + 1字节数据结果。

顺便说一句,如果我在这种情况下过滤掉1个字节,在所有其他情况下我的移动应用程序与Rails应用程序交互就好了。

如果有人能解释为什么我在响应中得到一个数据字节,我会很感激吗?

谢谢。

1 个答案:

答案 0 :(得分:2)

感谢@nickgrim回答这个问题,但是为了关闭这个问题,这里有解释......

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

很高兴知道我不会生气: - )

相关问题