如何支持向后兼容Rails 2.3.4中Accept头处理的更改 [英] How to support backwards compatibility with the changes to the Accept header handling in Rails 2.3.4

查看:169
本文介绍了如何支持向后兼容Rails 2.3.4中Accept头处理的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Rails 2.3.4中,处理 Accept 标题的方式已经改变:

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

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


我们不会接受

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

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].

同样值得注意的是,对您只声明了XML模板的操作的请求将不再为HTML请求(浏览器请求)自动呈现。这之前已经有效,不一定是设计,但是因为大多数浏览器发送了一个包含所有的Accept标头( / )。因此,如果要直接向浏览器提供XML,请确保提供:xml格式或显式指定XML模板(呈现template.xml)。

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,许多客户都在使用这些API发送 Content-Type 接受标头,两者都设置为 application / xml 。这工作正常,但我在Rails 2.3.4下的测试表明这不再有效 - 我得到了403 Unauthorized响应。删除接受标头,只发送 Content-Type ,但这显然不是一个可接受的解决方案,因为它将要求我的所有客户端重新编写他们的应用程序。

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.

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

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?

推荐答案

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

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

正如coderjoe正确指出的那样,根本不需要设置 Content-Type 标题 - 仅设置接受标题。

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

这篇关于如何支持向后兼容Rails 2.3.4中Accept头处理的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆