You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

HTTP内容协商疑问:请求Content-Type与响应格式及406错误的关系

Clarifying Content Negotiation: Accept vs Content-Type and 406 Responses

Great question—this is a super common point of confusion because Accept and Content-Type headers get mixed up all the time, but they serve totally distinct purposes in HTTP. Let’s break this down clearly:

First: What’s the real job of Content-Type in requests?

The Content-Type request header describes the format of the data you’re sending to the server, not the format you want to receive back. For example:

  • If you’re POSTing a JSON payload to an API, you’d send Content-Type: application/json to tell the server, "Hey, this request body is JSON—parse it accordingly."
  • It has zero to do with specifying what format you want in the response—that’s exclusively the job of the Accept header.

Your Core Questions, Answered

  1. If a request incorrectly uses Content-Type to "specify desired response format," must the server’s Content-Type match?
    No. Strictly speaking, servers don’t interpret Content-Type as a signal for content negotiation. They only use this header to parse your incoming request body. The server will either return its default response format, or rely on valid content negotiation headers (like Accept, if you included it) to pick a response type.

  2. If we fix this to using Accept properly: must the server’s Content-Type match, or return 406?
    Per the HTTP specification, yes—if the server can’t generate a response that matches any media type listed in your Accept header, it should return a 406 Not Acceptable status code. But as you noted, most real-world web apps skip this. They fall back to a default format (usually HTML) because regular users would have no clue what a 406 error means. It’s a pragmatic deviation from the strict spec, driven by usability.

Quick Cheat Sheet to Avoid Mix-Ups

  • Use Accept in requests to tell the server what response format you want.
  • Use Content-Type in requests to tell the server what format your request body is in.
  • Servers only use Accept (and related headers like Accept-Language) for content negotiation—Content-Type never factors into that process.

内容的提问来源于stack exchange,提问作者RuiDTLima

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:28:19