Skip to the workspace
Pebellslearn by building
Learn the Basics  ›  Capstone Project

Content Negotiation

Content Negotiation 14 minThe client lists the formats it can accept in the Accept header, labels any body it sends with Content-Type, and the server answers in the best format both sides support.

api design / Content Negotiation

Explore each scene at your own pace.

01 · The rule

The rule

Content Negotiation

The BookNook phone app wants JSON. A reporting tool wants CSV of the same loans. A French user wants error messages in French. You could build three separate endpoints, but HTTP already has a better way: the client says what it can accept, and the server picks the best match it can produce.

The rule

Content-Type says what I'm sending; Accept says what I'd like back.

Four headers do almost all the work:

  • Content-Type labels the body of this message: application/json, text/csv, application/xml.
  • Accept lists the formats the client can read, optionally ranked with q weights from 0 to 1.
  • Accept-Language does the same for human languages, like fr, en;q=0.5.
  • Accept-Encoding lists compressions the client understands, like gzip, br.
GET /v1/members/7/loans HTTP/1.1
Accept: text/csv, application/json;q=0.8

HTTP/1.1 200 OK
Content-Type: text/csv
Vary: Accept

id,book_id,due
901,42,2026-10-09

The client preferred CSV (weight 1, the default) over JSON (0.8). The server can produce CSV, so it sends CSV and labels it with Content-Type. The Vary: Accept header tells caches that this URL has several versions depending on Accept, so they must not hand a CSV response to a JSON client.

When negotiation fails, two status codes name the side that failed:

  • 406 Not Acceptable: the server cannot produce anything on the client's Accept list.
  • 415 Unsupported Media Type: the client sent a body in a format the server will not read.
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.