Managing HTTP Headers

1. Using Content-Type Header

ValueUse Case
application/jsonJSON request/response body
application/xmlXML body
application/x-www-form-urlencodedHTML form submission
multipart/form-dataFile uploads
application/octet-streamBinary data
application/merge-patch+jsonJSON Merge Patch (RFC 7396)
application/json-patch+jsonJSON Patch (RFC 6902)
text/event-streamServer-Sent Events

2. Using Accept Header

HeaderMeaning
Accept: application/jsonClient wants JSON
Accept: application/json, application/xml;q=0.9Prefer JSON, fallback XML
Accept: */*Any type
Accept: application/vnd.example.v2+jsonVendor-specific (versioning)

3. Using Authorization Header

SchemeFormat
BasicAuthorization: Basic base64(user:pass)
BearerAuthorization: Bearer eyJhbGciOi...
API KeyAuthorization: ApiKey abc123 or X-API-Key: abc123
DigestAuthorization: Digest username="...", realm="...", ...
AWS SigAuthorization: AWS4-HMAC-SHA256 ...

4. Using Cache-Control Header

DirectiveEffect
publicCacheable by any cache (including CDN)
privateCacheable only by browser, not shared caches
no-cacheMust revalidate with origin before serving
no-storeDo not cache at all
max-age=NFresh for N seconds
s-maxage=NShared cache max age (overrides max-age for CDN)
must-revalidateStale responses must revalidate
immutableResource will never change
stale-while-revalidate=NServe stale for N sec while refreshing

5. Using ETag Header

TypeExampleComparison
StrongETag: "33a64df5"Byte-for-byte identical
WeakETag: W/"33a64df5"Semantically equivalent

6. Using If-None-Match Header

RequestServer Action
If-None-Match: "abc"If current ETag = "abc" → 304; else 200 + new body
If-None-Match: *POST/PUT only if resource doesn't exist (insert-only)

7. Using If-Match Header

RequestServer Action
If-Match: "abc"Apply mutation only if ETag matches; else 412 Precondition Failed

Example: Optimistic Concurrency with If-Match

PUT /users/42 HTTP/1.1
If-Match: "v3-a1b2c3"
Content-Type: application/json

{"name": "Alice"}

# If current ETag is "v3-a1b2c3" → 200 OK
# Otherwise                       → 412 Precondition Failed

8. Using If-Modified-Since Header

RequestServer Action
If-Modified-Since: Sat, 29 Oct 2025 19:43:31 GMTIf unchanged since → 304; else 200

9. Using If-Unmodified-Since Header

RequestServer Action
If-Unmodified-Since: ...Apply mutation only if not modified since; else 412

10. Using Location Header

StatusMeaning
201 CreatedURI of newly created resource
202 AcceptedURI to poll for operation status
3xx RedirectNew URI to follow

11. Using X-RateLimit Headers

HeaderMeaning
X-RateLimit-LimitTotal requests allowed in window
X-RateLimit-RemainingRequests left in current window
X-RateLimit-ResetUnix timestamp when window resets
Retry-AfterSeconds (or HTTP date) before retry allowed
RateLimit-Policy NEWRFC 9239 standardized policy

12. Using CORS Headers

HeaderPurpose
Access-Control-Allow-OriginAllowed origin(s) or *
Access-Control-Allow-MethodsAllowed HTTP methods
Access-Control-Allow-HeadersAllowed request headers
Access-Control-Allow-Credentialstrue to send cookies
Access-Control-Max-AgePreflight cache duration (seconds)
Access-Control-Expose-HeadersHeaders JS can read

13. Using Custom Headers

ConventionNote
X- prefixDeprecated by RFC 6648 — use plain custom names
X-Request-IDCorrelation/trace ID (de facto standard)
X-Forwarded-ForOriginal client IP behind proxy
X-Forwarded-ProtoOriginal protocol (http/https)
Idempotency-KeyClient-generated key for safe retries
X-API-VersionAPI version selector