【问题标题】:HTTP POST, head :ok returning 1 mystery byteHTTP POST,head :ok 返回 1 个神秘字节
【发布时间】:2011-08-15 10:55:49
【问题描述】:

我有一个通过移动应用程序访问的 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 应用程序交互都很好。

如果有人能解释为什么我在响应中得到一个字节的数据,我将不胜感激?

谢谢。

【问题讨论】:

  • 这是为了解决 Safari 中的一个错误。看看stackoverflow.com/questions/3351247/…
  • 啊!谢谢,这就解释了。如果您想添加您的评论作为答案,我会这样标记。再次感谢。

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


【解决方案1】:

感谢@nickgrim 回答了这个问题,但为了结束这个问题,这里提供了解释......

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

很高兴知道我没有发疯 :-)

【讨论】:

  • 您可能需要考虑发布您引用的链接的全部内容。即使目标页面不再可访问,这也保证了解决方案的可用性。
猜你喜欢
  • 2019-12-01
  • 2011-03-03
  • 2015-03-11
  • 1970-01-01
  • 2020-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-16
相关资源
最近更新 更多