What the Prefer header is for
The HTTP Prefer request header, defined in RFC 7240, lets a client express
optional preferences about how the server should handle a request. Crucially,
a preference is advisory: the server may apply it, partially apply it, or ignore
it entirely, and the request must still succeed either way. This makes Prefer
safe to send speculatively — you never break a request by asking for a behaviour
the server does not support.
The registered preference tokens
| Token | Effect when applied | Common use case |
|---|---|---|
return=minimal | Server sends the smallest useful body (often 204 + Location) | Saving bandwidth on create/update |
return=representation | Server echoes the full resulting resource in the response | Save a follow-up GET after writing |
respond-async | Server replies with 202 Accepted + status URL immediately | Long-running operations |
wait=N | Server may block up to N seconds before switching to async | Bound latency on async operations |
handling=strict | Reject the request if any part cannot be processed | Strict validation |
handling=lenient | Do the best you can, ignore unsupported parts | Tolerant parsing |
How it works
A Prefer header carries one or more comma-separated preference tokens. Each
token is either a bare keyword (respond-async) or a name=value pair
(return=minimal, wait=10). Multiple preferences combine in one header:
Prefer: respond-async, wait=10, return=minimal
When the server honours a preference, it lists the applied tokens in a
Preference-Applied response header so the client can confirm what happened.
The registered tokens are respond-async, return (minimal or
representation), wait (seconds), and handling (strict or lenient).
Unknown tokens are simply ignored.
Worked examples
Avoiding a second GET after creation
Without return=representation, creating a resource typically requires a follow-up request:
POST /items
→ 201 Created
Location: /items/42
The client then fetches /items/42 to get the full record. With the preference:
POST /items
Prefer: return=representation
→ 201 Created
Location: /items/42
Content-Type: application/json
{...full item object...}
One round-trip instead of two. This is especially useful for mobile clients where latency is a concern.
Async processing with bounded wait
For a request that might complete fast or might be queued:
POST /reports/generate
Prefer: respond-async, wait=5
The server processes synchronously for up to 5 seconds. If it finishes in time, it returns 200 OK with the report. If not, it returns 202 Accepted with a polling URL. The client always gets a deterministic response within 5 seconds.
Tips
A server that varies its behaviour on Prefer should include Vary: Prefer in its response so caches do not serve one client’s minimal body to another client that wanted the full representation. Pair respond-async with wait to bound how long the server may block before switching to an async 202 Accepted response. Absence of a token in Preference-Applied means the preference was not applied — design your client to tolerate either outcome.