What Is Content Negotiation in REST APIs?
Frequently Asked Questions
Content negotiation is the process where a client and server agree on the best representation of a resource. The client sends Accept headers listing preferred formats like JSON or XML, and the server responds with the closest supported format along with the matching Content-Type header.
The Accept header lists media types the client can process, optionally with quality values (q-values). For example, Accept: application/json, text/xml;q=0.9 tells the server to prefer JSON but accept XML if JSON is unavailable.
Server-driven negotiation lets the server choose the response format based on Accept headers. Agent-driven negotiation returns a 300 Multiple Choices response containing links to available formats, letting the client pick. Most REST APIs use server-driven negotiation.
The Vary header tells caches that the response varies by request headers like Accept. Without it, a CDN might cache a JSON response and serve it to an XML client. Always include Vary: Accept when serving multiple formats from one URL.
Yes, some APIs version resources through media types like application/vnd.example.v2+json. However, URL versioning remains more common for major breaking changes because it is clearer and easier to document.
Support at least application/json as the default. Add application/xml for enterprise clients, text/html for documentation and browsability, application/pdf for exports, and application/octet-stream for file downloads. Tailor types to your actual user base.