Working with Content Negotiation

1. Using Accept Header Negotiation

Accept ValueServer Response
application/jsonJSON body
application/xmlXML body
text/csvCSV export
*/*Server default
No match406 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-LanguageResolved
fr-CA, fr;q=0.9, en;q=0.5fr-CA if supported
dede-DE fallback
*Server default

4. Using Encoding Negotiation (Accept-Encoding)

EncodingRatioCPU
gzip3-4xLow
br (Brotli)4-5xMedium
zstd4-5xLow NEW
identity1xNone

5. Setting Up Media Type Priorities

Example: Server preference

produces:
  - application/json    # preferred
  - application/xml
  - text/csv
default_content_type: application/json

6. Implementing Quality Values

q valueMeaning
1.0Most preferred (default)
0.5Acceptable
0.1Last resort
0.0Not acceptable

7. Configuring Default Content Types

ContextDefault
REST APIapplication/json
HTML pagetext/html; charset=utf-8
gRPCapplication/grpc
Binaryapplication/octet-stream

8. Using Custom Negotiation Rules

Example: Vendor media type

Accept: application/vnd.acme.user.v2+json
       application/vnd.acme.user.v1+json;q=0.5

9. Configuring Fallback Content Types

If client requestsServer falls back to
Unsupported typeDefault or 406
No Accept headerServer default
Wildcard */*Most efficient format

10. Setting Up Charset Negotiation

CharsetUse
utf-8Modern default
iso-8859-1Legacy European
shift_jisJapanese legacy
gb2312Simplified Chinese legacy
Note: Always declare charset in Content-Type header to prevent client mis-decoding.