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.