http - 在客户端请求没有 Accept header 的情况下发送 406 是否有效 HTTP

标签 http

https://www.rfc-editor.org/rfc/rfc7231#section-5.3.2指出

A request without any Accept header field implies that the user agent will accept any media type in response.

还有 https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html指出

If no Accept header field is present, then it is assumed that the client accepts all media types. If an Accept header field is present, and if the server cannot send a response which is acceptable according to the combined Accept field value, then the server SHOULD send a 406 (not acceptable) response.

不过,在这种情况下发回 406 Not Acceptable 是否错误? 特别是,我们希望强制客户编写版本感知的客户端代码(我们的内容类型已修改版本)。

最佳答案

would it be wrong to send back a 406 Not Acceptable in such case?

简短的回答是,答案在您自己的问题中。一旦 RFC 2616 不再相关,请考虑 RFC 7231 中的引述:

A request without any Accept header field implies that the user agent will accept any media type in response.

RFC 7231 的同一段说明了在什么情况下可以使用 406 状态码:

If the header field is present in a request and none of the available representations for the response have a media type that is listed as acceptable, the origin server can either honor the header field by sending a 406 (Not Acceptable) response or disregard the header field by treating the response as if it is not subject to content negotiation.

查看406如何状态码定义:

6.5.6. 406 Not Acceptable

The 406 (Not Acceptable) status code indicates that the target resource does not have a current representation that would be acceptable to the user agent, according to the proactive negotiation header fields received in the request (Section 5.3), and the server is unwilling to supply a default representation.

关于http - 在客户端请求没有 Accept header 的情况下发送 406 是否有效 HTTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51631167/

相关文章:

perl - 如何在包含 application/x-www-form-urlencoded 数据的 perl 中发出 HTTP PUT 请求?

java - JDK11中HttpClient的失败重试机制

javascript - 如何等到 http 请求完成后再继续?

http - Golang ListenAndServeTLS 在浏览器中不使用 https 时返回数据

PHP 默认内容类型?

angularjs - 无法在angularjs中创建post请求包含crsf

http - 删除网页后,它仍然显示在搜索引擎中,为什么?

javascript - 在 Meteor (JavaScript) 中指定内容类型

php - Angular 应用程序不调用 php

http - 如何从中间件设置日志记录上下文?