liveBook · Manning

Prefer header

As explained in The Design of Web APIs, Second Edition

The Prefer header is an HTTP request header field that lets a client communicate preferences about how a server should process a request.

The Prefer request header lets a client ask an API to handle an operation in a particular way. It is useful when different consumers need different amounts of data or different execution modes from the same operation. Prefer is defined by RFC 7240; preferences such as model=summarized are choices defined by the API, not universal meanings of the header. section 13.5.1 section 14.7.4

Choose an account model

GET /accounts with Prefer: model=complete requests complete accounts rather than the usual summaries. A consumer needing less data can request Prefer: model=summarized where that model is offered. The response keeps Content-Type: application/json and uses Preference-Applied to indicate the model returned. Without a model preference, the operation returns its default model. section 13.5.1

Content negotiation is an alternative: a client selects a custom model-specific media type with Accept, and Content-Type identifies the returned type. Prefer keeps the familiar application/json media type. Both approaches can serve read, list, and search operations, but both are uncommon enough to warrant caution: make the available choices and default clear to consumers. section 13.5.1

A preference can change more than the shape of a read response:

  • For creation or update, Prefer: model=none or Prefer: return-no-content can omit resource data a consumer does not need. A creation can still return 201 Created with a Location header; model=summarized or model=minimal offers smaller representations. section 13.5.2
  • For a long-running simulation, Prefer: response=synchronous requests a response after completion: 201 Created with DONE. With response=asynchronous or no preference, the operation responds immediately with 202 Accepted and IN_PROGRESS. A synchronous option can simplify a limited consumer, but long waits make it less efficient; access can be restricted with a scope. section 14.7.4

Choose a preference when a genuine consumer need justifies another behavior. Specify its values, default, response indication, and any access restrictions; do not make every client wait for a long operation merely because some clients prefer a synchronous response. section 13.5.1 section 14.7.4

How The Design of Web APIs, Second Edition covers it

How deep it goes
Works through request preferences and their responses, but stops short of server-side header parsing or implementation.
Examples
Uses account reads, resource creation without returned data, and a simulation with synchronous or asynchronous responses.
What it assumes
Assumes familiarity with HTTP media types and resource models while illustrating custom preferences with concrete requests.
What you can do after
Apply it via the HTTP Prefer header to offer documented consumer choices while retaining appropriate defaults and response signals.
How it connects
Connects HTTP content negotiation, resource models, Preference-Applied, asynchronous operations, and access scopes.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Design of Web APIs, Second Edition. The section links point to the parts of the book it draws on.