PUT vs POST
In this section you will learn about the difference between PUT and POST Method.
PUT vs POST
PUTis idempotent, meaning that making the same request will always produce the same result.
POSTis not idempotent, so repeated requests may yield different outcomes.
PUTis typically used to update existing resources, or to create new resources at a specified, known URL.
On the other hand,
POSTis mainly used to create new resources, with the server providing the new URL.
POSTsend data in the request body.
Effect on Resources
PUTshould not create a new resource if one already exists at the given URI.
POSTcan create multiple resources or trigger a variety of other operations.
URL in Request
PUTrequest, the URL specifies the resource to be updated or created.
POSTrequests, the URL indicates where to send the data for processing.
Example of PUT vs POST:
PUT to update a resource:
POST to create a resource:
Choose PUT or POST
Use PUT when:
The client is responsible for determining the URI of the new or updated resource.
You are replacing an entire resource or creating a resource with a client-defined identifier.
The operation needs to be idempotent, ensuring that repeated requests will have the same effect as a single request.
Use POST when:
The server is responsible for assigning a new unique identifier for the created resource.
The operation does not have to be idempotent, or it involves creating new resources.
The action performed is complex or does not fit neatly into the CRUD model.
PUT is ideal when the client knows the exact location of the resource and wants to update it or create a new resource at that location.
POST is better suited for when the client is submitting data to be processed by the server, and the server will determine where the new resource will be located (specifying the new URL in its response).