On this page
An API Location header after POST tells a client where to find a resource or, in a redirect response, where to go next. For a successful creation, returning the new resource’s URL in Location gives clients a standard place to look; a JSON body can provide the same URL alongside useful resource data.
This guide explains when to use each, how clients should resolve the URL, and what to consider behind a reverse proxy. The HTTP examples are illustrative and were not executed. RFC 9110 could not be consulted in this run, so verify status-code details against the standard when finalizing your API contract.
Key Takeaways
- For a
201 Createdresponse, useLocationto identify the primary resource created when its URL differs from the POST target. - Include a JSON representation when the client needs resource data immediately. Returning the URL in both places can be useful if the values identify the same resource.
- A relative
Locationis resolved against the request URL. An absolute URL is convenient but must use the correct public origin. - A
202 Acceptedresponse means processing is not complete; document a status-monitor URL explicitly rather than treating it as a completed resource. - Do not build public URLs from untrusted
Hostor forwarding headers. Configure the public origin or restrict which proxy headers the application trusts.
Use Location when POST creates a resource
A common pattern is to POST to a collection and return 201 Created with the new resource’s URL in Location:
POST /v1/orders HTTP/1.1
Host: api.example.com
Content-Type: application/json
{"item":"notebook","quantity":2}
HTTP/1.1 201 Created
Location: /v1/orders/847
Content-Type: application/json
{"id":"847","item":"notebook","quantity":2,"url":"/v1/orders/847"}
Here, the POST target is the collection, while Location identifies the newly created order. The body gives the client an immediately useful representation, including a URL for clients that work primarily with JSON. The resource may still have subsequent business processing to complete; creation does not imply that every operation associated with it has finished.
For a 201 response, if Location is absent, the request’s target URI identifies the primary created resource. That can fit an operation whose target URI is itself the new resource, but it is usually ambiguous for a POST to a collection. Return Location in the collection example so the client does not have to infer the new URL from an ID.
A created resource might not be publicly readable. Location identifies it; it does not grant permission to access it or promise that an unauthenticated GET will succeed.
Advertisement
Location header, JSON body, or both?
| Response design | Useful when | Trade-off |
|---|---|---|
Location without a representation | The client only needs to know where the created resource is, or can fetch its details later. | A client needing the details may need another request. |
| JSON body with a resource URL | The client needs resource data or multiple related links in a consistent application-specific format. | The API must document the field’s meaning; generic HTTP clients will not necessarily interpret it. |
| Both | The client needs the representation now and benefits from the standard header. | The header and body link must remain consistent. |
For 201, prefer returning Location when there is a distinct primary resource URL, even if the body includes a link. A body can also include related links—for example, to an order’s payment or cancellation operation—but label those separately from the created resource’s own URL.
If no useful representation is needed, an illustrative response can have an empty body:
HTTP/1.1 201 Created
Location: /v1/orders/847
Content-Length: 0
Document whether the returned URL is canonical, what operations it supports, and what authorization those operations require. Avoid promising that the URL will be permanent unless the API actually guarantees that.
Choose the status before choosing the URL
The same header does not mean exactly the same thing for every response. Choose the status based on the operation’s outcome:
201 Created: The request created one or more resources.Location, if present, identifies the primary created resource.202 Accepted: The request was accepted for processing, but processing has not completed. If clients can poll a status endpoint, provide its URL and explain its lifecycle, polling behavior, and possible outcomes. A JSON field such asstatusUrlmakes that application-specific meaning explicit; do not assume clients will interpretLocationas a status monitor without documentation.200 OK: The POST succeeded and returned a response, but the operation is not necessarily a resource-creation operation. A body can describe the result or link to relevant resources.303 See Other: Direct the client to another URI usingLocation, commonly when the result should be retrieved separately. A client following the redirect can retrieve the target with GET or HEAD; automatic redirect behavior varies by client.
For an asynchronous operation, the monitor and eventual resource can be different URLs. For example, statusUrl might identify a job while a later job representation supplies the completed resource URL. Do not label the job URL as the created resource unless the job itself is the resource your API intends to expose.
Advertisement
Resolve and use the returned URL
Location can contain an absolute URI or a relative reference. A client resolves a relative reference against the URL of the request that produced the response—not against an arbitrary configured API root.
For the request URL https://api.example.com/v1/orders:
/v1/orders/847resolves tohttps://api.example.com/v1/orders/847.orders/847resolves tohttps://api.example.com/v1/orders/847because the finalorderssegment in the base URL is replaced.https://api.example.com/v1/orders/847is already absolute.
The trailing slash matters: resolving orders/847 against https://api.example.com/v1/orders/ instead produces a URL under /v1/orders/orders/847. Use a standard URI-reference resolver rather than string concatenation.
After resolution, the client can store the resource URL or use it for a documented operation, such as a GET. Treat the URL as an identifier supplied by the API rather than reconstructing it from the ID. If a client sends credentials when following a returned URL, it should first apply its origin and scheme policy; do not automatically forward credentials to an unexpected origin. Likewise, server-side clients that fetch returned URLs need appropriate outbound-request controls.
Absolute versus relative URLs behind a proxy
An absolute Location, such as https://api.example.com/v1/orders/847, is self-contained. It is useful when URLs are consumed outside the original request context, but the service must know the externally visible scheme, host, port, and path prefix. An internal address or an http URL emitted behind a TLS-terminating proxy can be unusable or misleading to clients.
A relative reference avoids embedding the origin, but it still needs the correct public path. For example, if a proxy exposes an application under /v1, returning /orders/847 points at the origin root, not necessarily at /v1/orders/847. Test against the externally visible request URL, including any proxy-added prefix and trailing slash.
For absolute URLs, prefer a configured public base URL. If deployment requires forwarded scheme or host information, accept it only from explicitly trusted proxies, and configure those proxies to remove or overwrite untrusted inbound forwarding headers. Validate allowed hosts and public schemes as appropriate. Do not assume that a client-supplied Host, Forwarded, or X-Forwarded-* value is safe for constructing a public link.
Common Mistakes
- Returning the collection URL as the new resource URL. For a collection POST, return the distinct created resource’s URL when one exists.
- Treating
Locationas authorization. A client still needs the required credentials and permissions. - Claiming completion with
201when work was only queued. Use a response that reflects the actual state and document how to monitor asynchronous work. - Making the body and header disagree. If both identify the created resource, ensure they resolve to the same URI.
- Concatenating URL strings. Relative paths, trailing slashes, ports, and proxy prefixes can change the result; use a URI resolver.
- Trusting request headers to determine the public origin. An attacker-controlled host or forwarding value can cause the API to emit misleading links.
Best Practices
- Document the status,
Locationsemantics, body shape, and authorization requirements for each creation endpoint. - Use a stable, configured public origin for absolute URLs, or return relative references that account for the public path prefix.
- Test the full deployment path: direct and proxied requests, TLS termination, path rewriting, and client resolution of relative references.
- For retryable creation requests, design and document a deduplication or idempotency strategy; receiving no response does not prove that the first POST failed to create a resource.
- Check that clients handle a missing or malformed
Locationaccording to the API contract, and avoid forwarding credentials to an unexpected destination.
Conclusion
For a POST that creates a distinct resource, 201 Created with a Location header is a clear default. Add a JSON representation when it helps the client, keep any duplicate resource URLs consistent, and verify resolution and public URL construction in the deployed environment.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.