406
api
version_error
ai_generated
true
406 不可接受:Accept 头中指定的 API 版本不受支持
406 Not Acceptable: API version specified in Accept header is not supported
ID: api/rest-api-version-header-missing-accept
90%修复率
89%置信度
1证据数
2023-09-12首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| REST API versioning via Accept header | active | — | — | — |
| ASP.NET Core 8.0 | active | — | — | — |
| Spring MVC 6.1 | active | — | — | — |
| Express.js 4.18 | active | — | — | — |
根因分析
客户端发送的 Accept 头包含的媒体类型中指定了服务器无法识别或不提供的 API 版本(例如 application/vnd.myapp.v2+json)。
English
Client sent an Accept header with a media type that includes an API version (e.g., application/vnd.myapp.v2+json) that the server does not recognize or does not serve.
官方文档
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/406解决方案
-
通过查看 API 文档或查询版本发现端点,验证服务器支持的确切版本字符串。使用正确的媒体类型,例如 v1 版本使用 application/vnd.myapp.v1+json。
-
如果 API 使用基于 URL 的版本控制方案(例如 /api/v1/),则切换为使用该方案而不是 Accept 头版本控制。许多 API 同时支持这两种方法。
无效尝试
常见但无效的做法:
-
70% 失败
Without the Accept header, the server may default to the latest version or return a generic response, but this defeats version negotiation and may cause unexpected behavior.
-
80% 失败
The server may interpret application/json as the latest version, but if the client specifically needs an older version, this will not work and may break backward compatibility.