What is content negotiation in REST APIs?
Answer
Content negotiation is the mechanism by which a client and server agree on the format of the response. The client sends an Accept header listing preferred media types: Accept: application/json, application/xml;q=0.9. The server selects the best matching format it supports and sets Content-Type in the response accordingly. If the server cannot satisfy the Accept header, it returns 406 Not Acceptable. Most modern APIs return only JSON and can ignore the Accept header, but supporting content negotiation is important for enterprise APIs serving multiple client types. Similarly, the client declares its request body format with Content-Type: application/json and the server responds with 415 Unsupported Media Type if it cannot process that format.
Previous
What is the difference between a collection resource and a singleton resource?
Next
What is a RESTful API's uniform interface constraint?
More REST API Design Questions
View all →- Beginner What is REST and what are its six architectural constraints?
- Beginner What are the main HTTP methods used in REST APIs and what do they do?
- Beginner What is idempotency and which HTTP methods are idempotent?
- Beginner What are the most important HTTP status codes in REST APIs?
- Beginner What are REST resource naming conventions?