api protocol_error ai_generated true

406 不可接受:请求的媒体类型不受服务器支持

406 Not Acceptable: requested media type not supported by server

ID: api/http-406-not-acceptable-content-negotiation

其他格式: JSON · Markdown 中文 · English
90%修复率
88%置信度
1证据数
2023-11-05首次发现

版本兼容性

版本状态引入弃用备注
HTTP/1.1 (RFC 7231) active
Spring Boot 3.0+ active
ASP.NET Core 8.0+ active

根因分析

客户端的 `Accept` 头指定了服务器无法生成的媒体类型(例如 `application/xml`),导致内容协商失败。

English

The client's `Accept` header specifies a media type (e.g., `application/xml`) that the server cannot produce, causing content negotiation to fail.

generic

官方文档

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/406

解决方案

  1. 将 `Accept` 头设置为受支持的类型,通常是 `application/json`。示例:`curl -H "Accept: application/json" https://api.example.com/resource`。
  2. 查看服务器 API 文档以了解支持的媒体类型,并相应更新客户端。示例:`GET /api/v1/resource` 使用 `Accept: application/vnd.api+json`。
  3. 如果您控制服务器,通过配置内容协商序列化器(例如,在 Spring Boot 中添加 `jackson-dataformat-xml` 依赖)来添加对请求媒体类型的支持。

无效尝试

常见但无效的做法:

  1. 60% 失败

    The server may still default to a supported type (e.g., `application/json`), but if the client explicitly requires a type, removing the header changes behavior and may cause other issues.

  2. 80% 失败

    The server must explicitly support the requested type; the error is correct.

  3. 70% 失败

    While `*/*` may work, it bypasses content negotiation and may return an unexpected format.