如何支持与 Rails 2.3.4 中 Accept 标头处理更改的向后兼容性

发布于 2024-08-08 21:34:38 字数 1066 浏览 0 评论 0原文

在 Rails 2.3.4 中,处理 Accept 标头的方式已更改:

http://github.com/rails/rails/commit/1310231c15742bf7d99e2f143d88b383c32782d3

我们不会接受

Rails 处理传入 Accept 标头的方式已更新。这主要是因为网络浏览器似乎并不总是知道他们想要什么……更不用说能够一致地表达它了。因此,Accept 标头现在仅用于 XHR 请求或单个项目标头 - 这意味着它们不会请求所有内容。如果失败,我们将转而使用 params[:format]。

还值得注意的是,对仅声明 XML 模板的操作的请求将不再自动为 HTML 请求(浏览器请求)呈现。这以前是有效的,不一定是设计使然,而是因为大多数浏览器都会发送一个包罗万象的 Accept 标头(“/”)。因此,如果您想直接向浏览器提供 XML,请务必提供 :xml 格式或显式指定 XML 模板(呈现“template.xml”)。

我有一个活跃的 API,许多客户端都在使用它,这些客户端都发送 Content-TypeAccept 标头,两者都设置为 application/xml< /代码>。这工作正常,但我在 Rails 2.3.4 下的测试表明这不再工作——我收到 403 Unauthorized 响应。删除 Accept 标头并仅发送 Content-Type 即可,但这显然不是可接受的解决方案,因为它将要求我的所有客户重新编码其应用程序。

如果我继续部署到 Rails 2.3.4,所有使用该 API 的客户端应用程序都将崩溃。如何修改我的 Rails 应用程序,以便我可以继续在 Rails 2.3.4 上提供现有 API 请求,而无需客户端更改其代码?

In Rails 2.3.4, the way Accept headers are handled has changed:

http://github.com/rails/rails/commit/1310231c15742bf7d99e2f143d88b383c32782d3

We won't Accept it

The way in which Rails handles incoming Accept headers has been updated. This was primarily due to the fact that web browsers do not always seem to know what they want ... let alone are able to consistently articulate it. So, Accept headers are now only used for XHR requests or single item headers - meaning they're not requesting everything. If that fails, we fall back to using the params[:format].

It's also worth noting that requests to an action in which you've only declared an XML template will no longer be automatically rendered for an HTML request (browser request). This had previously worked, not necessarily by design, but because most browsers send a catch-all Accept header ("/"). So, if you want to serve XML directly to a browser, be sure to provide the :xml format or explicitly specify the XML template (render "template.xml").

I have an active API which is being used by many clients who are all sending both a Content-Type and an Accept header, both set to application/xml. This works fine, but my testing under Rails 2.3.4 demonstrates that this no longer works -- I get a 403 Unauthorised response. Remove the Accept header and just sending Content-Type works, but this clearly isn't an acceptable solution since it will require that all my clients re-code their applications.

If I proceed to deploy to Rails 2.3.4 all the client applications which use the API will break. How can I modify my Rails app such that I can continue to serve existing API requests on Rails 2.3.4 without the clients having to change their code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

饭团 2024-08-15 21:34:38

如果我理解正确的话,问题出在请求标头中。您只需添加一个自定义 Rack 中间件即可纠正它。

快速想法:

class AcceptCompatibility
  def initialize(app)
    @app = app
  end

  def call(env)
    if env['Accept'] == "application/xml" && env['Content-Type'] == "application/xml"
      # Probably an API call
      env.delete('Accept')
    end
    @app.call(env)
  end
end

然后在您的环境中。rb

require 'accept_compatibility'
config.middleware.use AcceptCompatibility

If I understand correctly the problem is in the Request headers. You can simply add a custom Rack middleware that corrects it.

Quick idea:

class AcceptCompatibility
  def initialize(app)
    @app = app
  end

  def call(env)
    if env['Accept'] == "application/xml" && env['Content-Type'] == "application/xml"
      # Probably an API call
      env.delete('Accept')
    end
    @app.call(env)
  end
end

And then in your environment.rb

require 'accept_compatibility'
config.middleware.use AcceptCompatibility
┾廆蒐ゝ 2024-08-15 21:34:38

令人尴尬的是,这实际上是 Apache 配置问题。一旦我解决了这个问题,一切都按预期进行。对此感到抱歉。

正如 coderjoe 正确指出的那样,根本不需要设置 Content-Type 标头——只需设置 Accept 标头。

Embarrassingly enough, this actually turned out to be an Apache configuration issue. Once I resolved this, everything worked as expected. Sorry about that.

As coderjoe correctly pointed out, setting the Content-Type header isn't necessary at all -- only setting the Accept header.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文