liveBook · Manning

HTTP header

As explained in The Design of Web APIs, Second Edition

A response header is a metadata field returned in an HTTP response that conveys protocol, representation, or application information.

An HTTP response header is a name:value metadata field returned after the status line and before the response body. It communicates protocol, representation, security, caching, or application information without placing that data in the body. section 4.6.1 section 5.1.1

Choose a response header when the output is metadata or fits a standard HTTP field; keep resource representations and error information in the body.

  • Return a newly created resource’s URL in the standard Location header with a 201 Created response. section 4.6.3 section 4.9
  • Avoid hiding substantial resource data in headers: product details, product lists, empty lists, and error information belong in the response body. section 4.6.3
  • Prefer standard fields from the IANA HTTP Field Registry before introducing custom headers. section 4.4.1 section 4.9

Documenting a Location response header

A product-creation response can describe its required Location header in OpenAPI: responses: "201": { headers: { Location: { description: "Product URL", required: true, schema: { type: string } } } }. The header identifies the created resource, while the response body can contain the product representation. section 6.8.3 section 7.6.3

Figure 4.16 There are only two locations for data in an HTTP response: headers and body.
figure

Use the response layout to distinguish its two data locations: headers for metadata and the body for general response data. section 4.6.1

How The Design of Web APIs, Second Edition covers it

How deep it goes
Explains placement, standard-field selection, Location usage, OpenAPI declaration, JSON Schema typing, and compatibility risks.
Examples
Uses a product-creation response whose required Location header contains the created product URL and has a string schema.
What it assumes
Assumes familiarity with HTTP request and response structure, status codes, resource representations, and basic OpenAPI documents.
What you can do after
Apply OpenAPI and JSON Schema to describe response headers and choose appropriate locations for output data.
How it connects
Connects HTTP responses with response bodies, HTTP status codes, Location, OpenAPI, JSON Schema, and backward compatibility.

Also explained in

Compare how 4 books explain HTTP header →

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.