1. GET and POST
1. GET method
Features:
- use: Used to obtain data from the server.
-
Parameter transfer method: The parameter will be appended to the URL to
key=value
The form is passed through query strings, for example:
/page?name=John&age=30
- Visibility: The parameters are plain text, and users can directly see the passed parameters in the browser address bar.
- limit: Due to the URL length limitation (specific restrictions vary by browser and server, but generally around 2,000 characters), the amount of data transmitted is relatively small.
- Idepotency: GET requests are idempotent, and repeatedly sending the same GET request will not change the state of the server.
- cache: The data requested by the GET can be cached by the browser or saved in the browser history.
advantage:
- Simple and intuitive, suitable for obtaining data.
- It can be saved through bookmarks for easy sharing.
shortcoming:
- Parameters are exposed to URLs, and sensitive data (such as passwords) are not secure.
- The amount of data is limited and large data cannot be uploaded.
2. POST method
Features:
- use: Used to submit data to the server, such as form submission or upload files.
- Parameter delivery method: Parameters are contained in the request body (Body), usually implicit. For example:
POST /form HTTP/1.1 Host: Content-Type: application/x-www-form-urlencoded Content-Length: 27 name=John&age=30
- Visibility: The parameters are not displayed in the URL, and the user cannot see the specific transferred content.
- limit: There is almost no data size limit, and large files can be transferred.
- Idepotency: POST requests are not idempotent, and each request will have a different impact on the server resources (such as adding new data).
- cache: POST requests will not be cached by the browser by default.
advantage:
- The security is slightly higher than that of GET (the parameters are not directly exposed to the URL, but the data is still protected with HTTPS).
- Supports the transmission of large amounts of data, suitable for complex data submissions.
shortcoming:
- It is not as intuitive as GET, and cannot be shared directly through URLs.
- Compared to GET, the performance may be slightly lower (requires more resources to handle the request body).
3. Summary and comparison
property | GET | POST |
---|---|---|
use | Get data | Submit data |
Parameter pass | URL query string | Body |
Security | Parameters are exposed plain text and are not suitable for sensitive data | Parameters are hidden, but HTTPS protection is required |
Data size | Limited by URL length | No obvious limitations |
Idepotency | Idesity | Non-idempotent |
cache | Cacheable | Not cacheable |
Visibility | The parameters are visible | Parameters are not visible |
Select method:
- GET: Suitable for scenarios where data is only obtained and no side effects, such as loading web page content.
- POST: Suitable for submitting form data, uploading files or other scenarios where server data needs to be modified.
2. PUT and DELETE
PUTandDELETEThese are two methods in the HTTP protocol, mainly used to perform modification and deletion operations on resources. They andGET、POSTLikewise, they are basic methods defined in HTTP/1.1 and are often used in RESTful API.
1. PUT method
Features
-
Purpose: Used to create or update resources.
- If the specified resourceDoes not exist, create a new resource.
- If the specified resourceAlready exists, the resource is updated.
Idepotency: PUT is idempotent, and repeatedly sending the same PUT requests will not have additional impact on the server's status (the same data will overwrite existing resources).
Request body: The PUT request body contains data that needs to be created or updated.
Example
Scene: Create or update user information.
ask:
PUT /users/123 HTTP/1.1 Host: Content-Type: application/json Content-Length: 47 { "username": "johndoe", "email": "john@" }
- Target resources:
/users/123
(Resource with user ID 123). - Request body: Contains user data.
response:
- Successfully created the resource:
201 Created
- Resources updated successfully:
200 OK
2. DELETE method
Features
- use: Used to delete the specified resource on the server.
- Idepotency:DELETE is also idempotent. Send multiple DELETE requests to the same resource, and the result is the same: the resource is deleted (or no longer exists).
- Request body: Usually no request body is needed, resource information is specified by the URL.
Example
Scene: Delete user information.
ask:
DELETE /users/123 HTTP/1.1 Host:
- Target resources:
/users/123
(Resource with user ID 123).
response:
- Deletion successfully:
200 OK
or204 No Content
- The resource does not exist:
404 Not Found
3. The difference between PUT and DELETE
property | PUT | DELETE |
---|---|---|
use | Create or update a resource | Delete resources |
Idepotency | Idesity | Idesity |
Request body | Contains resource data for creation or update | Usually no request body is required |
Response status code |
200 OK (Update successfully),201 Created (Create successfully) |
200 OK ,204 No Content ,or404 Not Found
|
Impact on the server | Write operations (add or overwrite resources) | Delete operation |
4. Comparison between PUT and POST
-
PUTis idempotent, usually specifying the full path to the resource (e.g.
/users/123
),emphasizeResource replacement。 -
POSTNot idempotent, usually operates on resource collections (such as
/users
),emphasizeNew resources。
5. RESTful API Practical Recommendations
- PUT: Used toAlready have resourcesMake modifications, or create a resource with the resource path known.
- DELETE: Used to delete specified resources and ensure resource uniqueness.
- POST: Used to add new resources, especially when the path is unknown or the creation rules are complex.
3. Summary
1. GET (read data)
- effect:Get data from the server
- Parameter position: Pass parameters through the query string of the URL.
- Request body: No request body.
- Idepotency: Idempotropic, the result of multiple requests is the same.
- Applicable scenarios: Load web page content, obtain user information, etc.
- Example:
GET /users/123 HTTP/1.1
2. POST (create data)
effect:Submit data to the server, usually used to create new resources
Parameter position: Data is passed in the request body.
Request body: There is a request body (such as form data or JSON).
Idepotency: Non-idempotent, new resources may be created per request.
Applicable scenarios: Submit forms, add new users, upload files, etc.
Example:
POST /users HTTP/1.1 Content-Type: application/json { "username": "johndoe", "email": "john@" }
3. PUT (create or update data)
effect:Create or update a specified resource
Parameter position: Data is passed in the request body.
Request body: There is a request body (including complete resource data).
Idepotency: Idempotropic, the results of multiple requests are consistent.
Applicable scenarios: Update user information, replace files, etc.
Example:
PUT /users/123 HTTP/1.1 Content-Type: application/json { "username": "newname", "email": "new@" }
4. DELETE (delete data)
effect:Delete specified resource from the server
Parameter position: The resource is specified via the URL.
Request body: Usually no request body.
Idepotency: Idempogenic, multiple requests have the same result (resources are deleted).
Applicable scenarios: Delete users, delete files, etc.
Example:
DELETE /users/123 HTTP/1.1
Summary comparison table
method | effect | Request body | Idepotency | Applicable scenarios |
---|---|---|---|---|
GET | Query data | none | yes | Get data, such as user information. |
POST | Create a resource | have | no | Create new resources, such as adding new users. |
PUT | Update (or create) a resource | Yes (full resource data) | yes | Update or replace resources, such as modifying users. |
DELETE | Delete resources | None (General) | yes | Delete a specified resource, such as deleting a user. |
Simply put:
- GET= Check.
- POST= Increase.
- PUT= Change (or overwrite).
- DELETE= Delete.
4. HTTP status code collection
Status code | coding | describe |
---|---|---|
1xx Informational Response | ||
100 | Continue | Continue, the client should continue its request |
101 | Switching Protocols | The server is switching protocols |
102 | Processing | The server has received the request, but has not processed yet (WebDAV) |
103 | Early Hints | Early tips for preloading resources |
2xx successfully responded | ||
200 | OK | Request succeeded |
201 | Created | Request succeeded and resource was created |
202 | Accepted | The request has been accepted, but has not been processed yet |
203 | Non-Authoritative Information | The request was successful, but the returned content may be from a third party |
204 | No Content | The request succeeded, but no content returned |
205 | Reset Content | The request is successful, and the client should reset the document view |
206 | Partial Content | The server successfully processed some requests (renewal of breakpoints) |
207 | Multi-Status | Return multiple status codes (WebDAV) |
208 | Already Reported | Resources have been reported (WebDAV) |
226 | IM Used | The server has completed the GET request and used the instance operation |
3xx redirection | ||
300 | Multiple Choices | There are multiple possible responses to the request |
301 | Moved Permanently | Resources have been moved permanently |
302 | Found | Resources temporarily moved (formerly called "Moved Temporarily") |
303 | See Other | The resource can be obtained at another URL |
304 | Not Modified | Resources have not been modified, cache can be used |
305 | Use Proxy | Must be accessed through proxy (deprecated) |
306 | Switch Proxy | This status code has been abandoned |
307 | Temporary Redirect | Temporary redirection, request method remains unchanged |
308 | Permanent Redirect | Permanent redirect, request method remains unchanged |
4xx client error | ||
400 | Bad Request | Client request format error |
401 | Unauthorized | Identity authentication is required |
402 | Payment Required | Status code reserved, not widely used |
403 | Forbidden | The server rejects the request |
404 | Not Found | The requested resource does not exist |
405 | Method Not Allowed | The request method is prohibited |
406 | Not Acceptable | The server cannot meet the requested content format |
407 | Proxy Authentication Required | Proxy authentication is required |
408 | Request Timeout | Request timeout |
409 | Conflict | Resource conflicts (such as concurrent modifications) |
410 | Gone | Resources have been permanently deleted |
411 | Length Required | Requires Content-Length |
412 | Precondition Failed | Request header condition failed |
413 | Payload Too Large | The request volume is too large and the server cannot process it |
414 | URI Too Long | The requested URI is too long |
415 | Unsupported Media Type | Unsupported media types |
416 | Range Not Satisfiable | The requested scope is invalid |
417 | Expectation Failed | The server cannot meet the Expect header requirements |
418 | I’m a teapot | Traditional April Fool's Day eggs, saying "I am a teapot" |
421 | Misdirected Request | The request was sent to the wrong server |
422 | Unprocessable Entity | Request format is correct, but cannot be processed (WebDAV) |
423 | Locked | Resource locked (WebDAV) |
424 | Failed Dependency | Dependency request failed (WebDAV) |
425 | Too Early | The server refuses to process a request that may be replayed |
426 | Upgrade Required | Need to upgrade the protocol |
428 | Precondition Required | Preconditions required |
429 | Too Many Requests | Too many requests sent by the client (current limit) |
431 | Request Header Fields Too Large | The request header field is too large |
451 | Unavailable For Legal Reasons | Not available due to legal reasons |
5xx Server Error | ||
500 | Internal Server Error | Internal server error |
501 | Not Implemented | The server does not support this request |
502 | Bad Gateway | An invalid response was received when the server acts as a gateway |
503 | Service Unavailable | The server is temporarily unavailable (overload or maintenance) |
504 | Gateway Timeout | When the server acts as a gateway, the upstream server response is not received in time. |
505 | HTTP Version Not Supported | The server does not support this HTTP version |
506 | Variant Also Negotiates | Server internal configuration error |
507 | Insufficient Storage | Insufficient server storage space (WebDAV) |
508 | Loop Detected | The server detects an infinite loop (WebDAV) |
510 | Not Extended | The server needs to extend the request |
511 | Network Authentication Required | Requires network authentication |
This form has been verified and complies with the latest HTTP standards.
This is the end of this article about the four HTTP methods (GET, POST, PUT and DELETE). For more related http get post put delete content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!