What Is a 201 Status Code?
The request has been fulfilled and has resulted in one or more new resources being created.
The primary resource created by the request is identified by either a Location header field in the response or, if no Location field is received, by the effective request URI.
The 201 response payload typically describes and links to the resource(s) created. See Section 7.2 of RFC7231 for a discussion of the meaning and purpose of validator header fields, such as ETag and Last-Modified, in a 201 response.
- Source: RFC7231 Section 6.3.2
201 CODE REFERENCES
Rails HTTP Status Symbol :created
Go HTTP Status Constant http.StatusCreated
Symfony HTTP Status Constant Response::HTTP_CREATED
Python2 HTTP Status Constant httplib.CREATED
Python3+ HTTP Status Constant http.client.CREATED
Python3.5+ HTTP Status Constant http.HTTPStatus.CREATED
.NET HttpStatusCode.Created
Rust http::StatusCode::CREATED
Java java.net.HttpURLConnection.HTTP_CREATED
Apache HttpComponents Core org.apache.hc.core5.http.HttpStatus.SC_CREATED
Angular @angular/common/http/HttpStatusCode.Created
201 status code example
Here’s an example of a request and response for a 201 status code:
Request
POST /api/v1/users HTTP/1.1
Host: example.com
Content-Type: application/json
{
"name": "John Doe",
"email": "[email protected]",
"password": "s3cr3t"
}
Response
HTTP/1.1 201 Created
Content-Type: application/json
Location: https://example.com/api/v1/users/123
Cache-Control: no-cache
{
"id": 123,
"name": "John Doe",
"email": "[email protected]",
"created_at": "2023-03-17T10:30:00Z"
}
In this example, the client is making a POST request to create a new user in the system. The server successfully creates the user and responds with a 201 Created status code, indicating that the request was successful and a new resource has been created. The response also includes a Location header that specifies the URL of the newly created resource, along with a JSON body containing the details of the user that was created, such as its ID, name, email, and creation date.
What is the difference between a 201 status code and a 200 status code?
The main difference between a 201 and 200 status code is the purpose of the response.
A 201 Created status code indicates that a new resource has been successfully created as a result of the client’s request. The server should include a Location header in the response to specify the URL of the newly created resource. This is often used in APIs where clients need to create new resources, such as adding a new user or creating a new file.
A 200 OK status code, on the other hand, indicates that the server has successfully fulfilled the client’s request, and the response body contains the requested data. This is often used for requests that retrieve or read data, such as fetching user details or retrieving a list of products.
In summary, a 201 status code is used when a new resource is created, while a 200 status code is used when the request is successful and data is returned.
Additional resources
- Learn about web development
- Learn about SEO
- Web development services from WebFX
- SEO services from WebFX
- MDN Web Docs
- W3Schools
Marketing Tips for Niche Industries
- Trucking Website Design Services That Help You Grow Online
- Web Design for PPE Companies: 5 Sales-Boosting Design Tips
- Web Design for Exercise Equipment Companies: Browse Services & Get Tips
- Web Design for Storage Facilities: Launch a Revenue-Driving Site
- Web Design for Pressure Washing Companies
- Web Design for Restoration Companies (+4 Tips for Success)
- Web Design for Flooring Companies – WebFX Design Services
- Web Design for Biotech Companies: How to Design a Biotech Site
- Web Design for Window and Door Companies: Inspire Your Site