The HTTP 201 status code stands for "Created". It is a successful response indicating that the request has been fulfilled, and a new resource has been successfully created on the server.
When is HTTP 201 Used?
- Typically used with POST or PUT requests when a new resource is created.
- The server should include a Location header specifying the URL of the newly created resource.
Example Scenarios
- User Registration – A user signs up, and a new account is created.
- Creating a New Database Record – A new entry is added to a database via an API.
- Uploading a File – A file is successfully uploaded to the server.
Example
Client Request (Creating a New User)
POST /users HTTP/1.1
Host: example.com
Content-Type: application/json
{ "username": "john_doe", "email": "john@example.com" }
Server Response:
HTTP/1.1 201 Created
Location: /users/123
Content-Type: application/json
{ "id": 123, "username": "john_doe", "email": "john@example.com" }
Summary
HTTP 201 is used when a new resource is successfully created. The server should include the resource's location in the response, allowing the client to retrieve it if needed.