HTTP Status Code 201, also known as "201 Created", means the request was successful and a new resource was created as a result. This response status is part of the 2xx (Success) class of HTTP codes.
HTTP Status Code 201, also known as "201 Created", means the request was successful and a new resource was created as a result. This response status is part of the 2xx (Success) class of HTTP codes.
It is commonly used in RESTful APIs when a POST request leads to the creation of a new entity. The server typically returns a Location header pointing to the newly created resource.
According to the HTTP/1.1 specification, "The newly created resource can be referenced by the URI(s) returned in the response, with the most specific URI given by a Location header field."
The 201 Created response is primarily used in situations where a new resource is generated as a result of the request. Below are some common use cases:
When a client submits a POST request to create a new user, order, or record in a database.
When a user uploads a file to a cloud storage service, and the server creates a new reference for the uploaded file.
When a user creates a new post, comment, or article, and the system generates a unique identifier for the content.
Here’s a simple example of how a POST request might return a 201 Created status code:
Request:
POST /users HTTP/1.1
Host: api.example.com
Content-Type: application/json
{
"name": "John Doe",
"email": "johndoe@example.com"
}
Response:
HTTP/1.1 201 Created
Location: https://api.example.com/users/123
Content-Type: application/json
{
"id": 123,
"name": "John Doe",
"email": "johndoe@example.com"
}
The Location header provides the URL of the newly created resource. Best practices suggest including a representation of the created resource in the response body, though it is not strictly required.
Understanding when to use 201 Created versus other similar status codes is crucial for API design.
Let’s check the versus!
Should I return a body with HTTP 201 Created?
Can 201 be used with PUT?
What is the difference between 201 and 202 Accepted?
To better understand how 201 Created works in a typical API workflow, consider the following diagram:
Understanding HTTP 201 Created is essential for designing effective APIs that provide clear and predictable responses when new resources are created. By following best practices—such as including a Location header and returning relevant details—you can ensure a smooth user experience and efficient API interactions.
If you're looking for tools to streamline your API development and improve response handling, check out AbstractAPI. Our suite of API services simplifies implementation, enhances data validation, and optimizes API performance.
Get started today and build more reliable applications!