How to use http status code 201

DDD
Release: 2023-10-13 15:07:29
Original
798 people have browsed it

Usage: 1. Create a resource. When the client sends a POST request, the server successfully processes the request and includes the URL of the newly created resource in the response; 2. Redirect, in this case, the response The header field Location will contain the redirected URL; 3. The response body usually contains information about the newly created resource, which can be the description, properties or other related data of the resource; 4. The optional response header field to provide More information about newly created resources.

How to use http status code 201

# Operating system for this tutorial: Windows 10 system, Dell G3 computer.

HTTP status code 201 indicates that the request was successful and the server has created a new resource. In this article, we will detail the usage and examples of HTTP status code 201.

HTTP status code is a three-digit code returned by the server when responding to an HTTP request. Status code 201 is part of the 2xx success category and indicates that the request was successful and a new resource was created.

The usage of HTTP status code 201 is as follows:

1. Create resource: Status code 201 is usually used to indicate that a new resource has been successfully created. When the client sends a POST request, the server successfully processes the request and includes the URL of the newly created resource in the response. This URL can be obtained through the response header field Location.

2. Redirect: In some cases, the server may use status code 201 to instruct the client to redirect. In this case, the response header field Location will contain the redirected URL.

3. Response body: The response body of status code 201 usually contains information about the newly created resource. This information can be the resource's description, properties, or other related data.

4. Optional response header fields: The server can optionally include additional header fields in the response to provide more information about the newly created resource.

The following is an example showing the use of HTTP status code 201:

POST /api/users HTTP/1.1 Host: example.com Content-Type: application/json Content-Length: 56 { "name": "John Doe", "email": "john@example.com" }
Copy after login
HTTP/1.1 201 Created Location: /api/users/123 Content-Type: application/json Content-Length: 87 { "id": 123, "name": "John Doe", "email": "john@example.com", "created_at": "2022-01-01 12:00:00" }
Copy after login

In the above example, the client sends a POST request to create a new user resource. The server successfully processes the request and returns status code 201. The response header field Location indicates that the URL of the newly created resource is /api/users/123. The response body contains details of the newly created resource, including user ID, name, email, and creation time.

The client can use the returned URL to access the newly created resource or perform further operations as needed.

Summary:

HTTP status code 201 indicates that the request was successful and the server has created a new resource. It is typically used to indicate the successful creation of a new resource and to provide information about the resource in the response. By using status code 201, the server can provide the client with the URL and other relevant data about the newly created resource so that the client can further operate or access the resource.

The above is the detailed content of How to use http status code 201. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!