内容协商中的规范类型与请求类型
因此,我想知道对于返回的数据可能具有多个有效 MIME 类型的请求,应该如何处理 HTTP 内容协商。
例如,如果说我有一些具有以下可能的 MIME 类型的任意数据:
text/data,application/x-data,application/data+xml
鉴于可能有多种 MIME 类型,从我的 Web 应用程序请求此表单数据的客户端应用程序应该合理地期望接收该类型的数据,无论是哪种类型他们在请求 Accept 标头中使用的 MIME 类型。但对于哪种类型是该数据的规范类型,通常会达成一些共识(即,一种是在 IETF 中正确注册的 MIME 类型,而其他类型是预注册或预标准化类型)。
所以我的问题是,我应该在回复这些请求时将返回的 Content-Type 标头设置为规范类型还是请求的类型?哪个是更好的做法?
我应该始终返回规范类型还是应该回显请求的类型以确保与尚未更新的较旧/编写不佳的应用程序兼容?目前,我正在返回规范类型,因为这似乎是一种更好的方法,也是大多数人对我正在使用的数据类型所做的事情。
这有点主观,所以如果您认为它应该是社区 wiki 而不是问题,请告诉我,我会考虑更改它
So I was wondering what should be done with HTTP Content Negotiation regarding requests where the returned data may have multiple valid MIME types.
For example if say I have some arbitrary data that has the following possible MIME types:
text/data,application/x-data,application/data+xml
Given that there are multiple MIME types possible a client application requesting data of this form from my web application should reasonably expect to receive that kind of data regardless of which of the MIME types they use in their requests Accept header. BUT there will generally be some consensus about which type is the canonical type for this data (ie. one will be a proper registered MIME type with the IETF while the others will be pre-registration or pre-standardisation types).
So my question is should I in replying to these requests set my returned Content-Type header to be the canonical type or the requested type? Which is better practice?
Should I always return the canonical type or should I echo back the requested type to ensure compatibility with older/poorly written apps that haven't been updated? Currently I'm returning the canonical type as this seems to be to be a better approach and what most people are doing with the data types I'm working with.
This is somewhat subjective so if you think it should be community wiki instead of a question let me know and I'll consider changing it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当用户代理(客户端应用程序)请求资源时,它们将为该单个请求的资源发出几种不同的 MIME 类型。发生这种情况是出于兼容性原因,以便让 Web 服务器知道用户代理能够接受什么。服务器的工作是确定资源的实际 MIME 类型。
在服务器端做出一些决定来找到适当的响应并没有什么坏处。例如,如果您希望将所有 HTML 页面作为 XML 提供服务,但主要的用户代理不支持必要的 mime 类型,您可以使用辅助技术(例如 PHP 或 ASP)提供动态响应。在此示例中,最好的想法是将正确的 mime 类型设置为规范,如果用户代理不支持规范,则为他们提供他们支持的其他内容,否则每个人都会获得规范。
When a user-agent (client application) requests a resource they will beacon out several different mime-types for that single requested resource. This occurs for compatibility reasons to let the web server know what the user-agent is capable of accepting. It is the job of the server to determine what the mime-type of the resource will actually be.
It doesn't hurt to make some decisions on the server side to in finding the appropriate response. If you wish to serve all HTML pages as XML, for instance, but a major user-agent does not support the necessary mime-type you can supply a dynamic response using an assisting technology, such as PHP or ASP. In the case of this example the best idea would be to set the correct mime-type as the canonical and if the user-agent does not support the canonical then serve them something else that they will support, otherwise everybody gets the canonical.