Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.

Translated from:

  • Stackoverflow.com/questions/6…
  • Stackoverflow.com/questions/4…
  • Stackoverflow.com/questions/4…

Question: What is the difference between POST and PUT in HTTP methods? What are the applicable scenarios for each of them?

You can find claims like this online:

  • POST is used to create resources, and PUT is used to modify resources
  • PUT should be used to create resources and POST should be used to modify resources

Neither is quite right.

A better approach is to choose between PUT and POST based on the idempotent nature of the action.

In this article, we will first introduce the concept of Idempotence and explain which HTTP methods are Idempotence. In the next article, we will analyze PUT and POST respectively and conclude.

Idempotence

Idempotence is a property of HTTP methods.

The request method is considered idempotent if multiple identical requests have the same expected effect on the server as a single request. It is important to note that idempotence affects the resource state of the server (that is, the state of the server after the request completes), not the response status code received by the client.

To illustrate this, consider a DELETE method defined as idempotent, such as a client making a request for a DELETE method, in order to DELETE a resource on a server, the server processing the request, the resource is deleted, and the server returns a status code of 204. The client then repeatedly sends the same DELETE request because the resource has been deleted, and the server returns 404.

Although the status codes received by clients differ, a single DELETE request has the same effect as multiple DELETE requests for the same URI.

Finally, if the client fails before it can read the server’s response, idempotent requests can be automatically retried. Clients know that repeated requests can produce the same expected results, although their responses may differ.

So to summarize, the core concept of idempotence is – you can send requests multiple times without making additional changes to the server state.

So which HTTP methods are idempotent?

Safe Methods

All safe methods are idempotent.

A method is safe if it is read-only to the server, meaning that it does not make changes to the server’s data. These methods are safe: GET, HEAD, and OPTIONS

Reference: www.rfc-editor.org/rfc/rfc7231…

Idempotent Methods

Methods like GET, HEAD, PUT, and DELETE are idempotent when implemented correctly, but POST is not. All safe methods are idempotent, too.

Reference: www.rfc-editor.org/rfc/rfc7231…

Summary of HTTP methods

As mentioned above, HTTP methods can be divided into the following types:

+---------+------+------------+ | Method | Safe | Idempotent | +---------+------+------------+ | CONNECT | no | no | | DELETE | no | yes | | GET | yes | yes | | HEAD | yes | yes | | OPTIONS | yes | yes | | POST | no | no | | PUT | no | yes  | | TRACE | yes | yes | +---------+------+------------+Copy the code