Working with Content Negotiation
| Accept Value | Server Response |
application/json | JSON body |
application/xml | XML body |
text/csv | CSV export |
*/* | Server default |
| No match | 406 Not Acceptable |
2. Configuring Content-Type Negotiation
Example: Request with Accept
GET /reports/2026 HTTP/1.1
Accept: application/json;q=0.9, text/csv;q=1.0, */*;q=0.1
HTTP/1.1 200 OK
Content-Type: text/csv
Vary: Accept
3. Implementing Language Negotiation (Accept-Language)
| Accept-Language | Resolved |
fr-CA, fr;q=0.9, en;q=0.5 | fr-CA if supported |
de | de-DE fallback |
* | Server default |
4. Using Encoding Negotiation (Accept-Encoding)
| Encoding | Ratio | CPU |
gzip | 3-4x | Low |
br (Brotli) | 4-5x | Medium |
zstd | 4-5x | Low NEW |
identity | 1x | None |
Example: Server preference
produces:
- application/json # preferred
- application/xml
- text/csv
default_content_type: application/json
6. Implementing Quality Values
| q value | Meaning |
| 1.0 | Most preferred (default) |
| 0.5 | Acceptable |
| 0.1 | Last resort |
| 0.0 | Not acceptable |
7. Configuring Default Content Types
| Context | Default |
| REST API | application/json |
| HTML page | text/html; charset=utf-8 |
| gRPC | application/grpc |
| Binary | application/octet-stream |
8. Using Custom Negotiation Rules
Accept: application/vnd.acme.user.v2+json
application/vnd.acme.user.v1+json;q=0.5
9. Configuring Fallback Content Types
| If client requests | Server falls back to |
| Unsupported type | Default or 406 |
| No Accept header | Server default |
Wildcard */* | Most efficient format |
10. Setting Up Charset Negotiation
| Charset | Use |
utf-8 | Modern default |
iso-8859-1 | Legacy European |
shift_jis | Japanese legacy |
gb2312 | Simplified Chinese legacy |
Note: Always declare charset in Content-Type header to prevent client mis-decoding.