Created
Production Risk
None. This is the desired outcome for successful resource creation.
The request has been fulfilled and has resulted in one or more new resources being created. The newly created resource can be referenced by the URI(s) returned in the entity of the response, with the most specific URI for the resource given by a Location header field.
- 1A client sends a POST request to create a new user account.
- 2An API call to create a new item in a database is successful.
- 3A file is successfully uploaded to a server via a PUT request.
An application sends a POST request to an API to add a new product to a catalog.
POST /api/products HTTP/1.1
Host: example.com
Content-Type: application/json
{ "name": "New Widget", "price": 19.99 }expected output
HTTP/1.1 201 Created
Fix
Return 201 with a Location header pointing to the new resource
// Express — correct use of 201 Created for a POST endpoint
app.post('/api/products', async (req, res) => {
const product = await db.products.create(req.body);
res
.status(201)
.location(`/api/products/${product.id}`)
.json(product);
});
// Client-side: read the Location header to find the new resource
const response = await fetch('/api/products', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'Widget', price: 9.99 }),
});
if (response.status === 201) {
const newProductUrl = response.headers.get('Location');
const product = await response.json();
}Why this works
201 Created signals that a new resource was successfully created as a result of the request. Including a Location header with the URI of the new resource is strongly recommended by RFC 7231 — it lets clients immediately reference the created item without a follow-up lookup. The response body should contain a representation of the new resource.
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev