gpt4 book ai didi

ruby-on-rails - 如何支持向后兼容 Rails 2.3.4 中对 Accept header 处理的更改

转载 作者:行者123 更新时间:2023-12-04 07:37:12 25 4
gpt4 key购买 nike

在 Rails 2.3.4 中,Accept header 的处理方式发生了变化:

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").

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

如果我继续部署到 Rails 2.3.4,所有使用该 API 的客户端应用程序都会中断。我如何修改我的 Rails 应用程序,以便我可以继续在 Rails 2.3.4 上处理现有的 API 请求,而无需客户更改他们的代码?

最佳答案

如果我理解正确,问题出在请求 header 中。您可以简单地添加一个自定义 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

关于ruby-on-rails - 如何支持向后兼容 Rails 2.3.4 中对 Accept header 处理的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1612633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com