HTTP内容协商疑问:请求Content-Type与响应格式及406错误的关系
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/jsonto 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
Acceptheader.
Your Core Questions, Answered
If a request incorrectly uses
Content-Typeto "specify desired response format," must the server’sContent-Typematch?
No. Strictly speaking, servers don’t interpretContent-Typeas 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 (likeAccept, if you included it) to pick a response type.If we fix this to using
Acceptproperly: must the server’sContent-Typematch, or return 406?
Per the HTTP specification, yes—if the server can’t generate a response that matches any media type listed in yourAcceptheader, it should return a406 Not Acceptablestatus 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
Acceptin requests to tell the server what response format you want. - Use
Content-Typein requests to tell the server what format your request body is in. - Servers only use
Accept(and related headers likeAccept-Language) for content negotiation—Content-Typenever factors into that process.
内容的提问来源于stack exchange,提问作者RuiDTLima

