201

Created

2xx · Success common

Overview

201 Created means the request succeeded and, as a result, a new resource was created. It is typically returned for POST or PUT, and the URL of the new resource is given in the Location header.

The response body commonly includes a representation of the created resource (for example a server-assigned id and creation time) so the client can reference it immediately.

When it happens

Request / Response example

Request
POST /api/articles HTTP/1.1
Host: api.example.com
Content-Type: application/json

{"title":"Hello","body":"First post"}
Response
HTTP/1.1 201 Created
Location: /api/articles/1024
Content-Type: application/json

{"id":1024,"title":"Hello"}

In code

app.post('/api/articles', (req, res) => {
  const id = db.insert(req.body);
  res.status(201).location(`/api/articles/${id}`).json({ id });
});
@app.post('/api/articles')
def create():
    aid = db.insert(request.json)
    return {'id': aid}, 201, {'Location': f'/api/articles/{aid}'}

Notes

Related status codes

Related headers

Specification