201 Created
The request succeeded and at least one new resource now exists because of it. The response says where that resource lives.
What 201 means
A 201 says two things: the request was fulfilled, and the result was a new resource. The address of that resource is given in the Location header. If there is no Location, the new resource is the URL the request was sent to.
The body normally describes the new resource and links to it. In a JSON API that usually means the stored record, including the fields the server filled in: the ID, timestamps and defaults.
An ETag or Last-Modified header on a 201 describes the representation that was just created, so the client can use it straight away for a conditional update.
POST and PUT use it differently
| Request | The new resource is | Response |
|---|---|---|
POST /orders | Chosen by the server | 201 with Location: /orders/8124 |
PUT /files/report.pdf, nothing there yet | The request URL itself | 201, Location optional |
PUT /files/report.pdf, replacing a file | Nothing new | 200 or 204 |
For PUT the rule is strict: a server must answer 201 when the PUT created the resource, and 200 or 204 when it replaced an existing one. For POST, 201 with a Location header is the recommended answer whenever resources were created.
Location may be a relative reference such as /orders/8124. Clients resolve it against the URL they sent the request to.
When 201 is the wrong code
- Nothing exists yet. If the request was put on a queue and the resource will appear later, send 202 Accepted with a URL where progress can be checked. A client that receives a 201 will often GET the
Locationimmediately, and that request must succeed. - Nothing new was made. An update, a search sent by POST or an action such as "send this email" should return 200 or 204.
- It already exists. A create request that collides with an existing record is a 409 Conflict, not a second 201.
- The input was rejected. Use 422 or 400; never create half a resource and report success.
Handling a 201 in a client
- Read the new address from
Location. Do not build it yourself from an ID in the body; the server decides its own URLs. - Accept any 2xx as success. Code that checks
status === 200treats a correct 201 as a failure. - Do not assume a body. Some APIs send only headers with a 201, so guard the JSON parsing.
- Keep the
ETagif there is one and send it back inIf-Matchwhen you later update the resource. - Be careful with retries. POST is not idempotent: if the connection drops before the 201 arrives, you cannot tell whether the resource was created. Look it up first, or use an idempotency key if the API offers one, or you may create a duplicate.
201 compared with 200, 202 and 204
| Code | Says | Use it when |
|---|---|---|
| 201 Created | Done, and something new exists | The resource is ready at the URL in Location |
| 200 OK | Done, here is the result | Nothing was created, or you replaced an existing resource |
| 202 Accepted | Received, not done yet | The creation happens later in a background job |
| 204 No Content | Done, nothing to add | An update or delete with no body to return |
Example 201 response
HTTP/1.1 201 Created
Location: /orders/8124
Content-Type: application/json
ETag: "a1f3"
Content-Length: 46
{"id":8124,"status":"pending","total":"49.00"}The client learns the new URL from Location, the stored record from the body and its current version from the ETag, all without a second request.
Defined in RFC 9110, section 15.3.2.
Questions
Does a 201 response need a Location header?
After a POST it should have one, because the client has no other way to learn the address the server chose. It is not strictly required: without it, the created resource is taken to be the URL the request was sent to, which is the normal case for PUT.
Should a 201 response include a body?
It is optional but useful. Returning the created record saves the client a follow-up GET and shows the values the server generated, such as the ID and timestamps.
Should PUT return 201 or 200?
Return 201 when the PUT created a resource that did not exist before, and 200 or 204 when it replaced an existing one. The HTTP specification requires that distinction.
What is the difference between 201 and 202?
201 means the resource exists now. 202 Accepted means the request was taken on but the work has not been done yet, so there may be nothing at the final URL for a while.