HTTP methods define the actions an API client wants to perform on a resource. Each method corresponds to a specific operation—such as creating, reading, updating, or deleting data—and must be included in every request to a REST API.
Out of the 39 HTTP methods, developers primarily use GET, POST, PUT, PATCH, and DELETE.
In this article, I'll break down the key differences between these five methods and explain when to use each one effectively.
1. GET
The GET method retrieves resources from a server without modifying them, making it a safe operation. It is also idempotent, meaning multiple requests will always return the same result.
Example URIs
HTTP GET 'http://www.sample-domain.com/users'
HTTP GET 'http://www.sample-domain.com/users?limit=20&page=2'
HTTP GET 'http://www.sample-domain.com/users/123'
HTTP GET 'http://www.sample-domain.com/users/123/address'
2. POST
POST method is used to create a new resource into the collection of resources on a server.
It is important to note that POST is Non-idempotent. Thus invoking two identical POST requests will result in duplicate information being created on the server.
Example URIs
HTTP POST 'http://www.sample-domain.com/users'
HTTP POST 'http://www.sample-domain.com/images'
3. PUT
PUT is used to update the existing resource on the server and it updates the full resource.
If the resource does not exist, PUT may decide to create a new resource.
PUT method is idempotent Thus calling this method multiple times will always update the same resource multiple times.
Example URIs
HTTP PUT 'http://www.sample-domain.com/users/123'
HTTP PUT 'http://www.sample-domain.com/images/123'
4. PATCH
PATCH is used to update the existing resource on the server and it updates a portion of the resource.
Example URIs
HTTP PATCH 'http://www.sample-domain.com/users/123'
HTTP PATCH 'http://www.sample-domain.com/images/123'
5. DELETE
DELETE Method is used to delete the resources from a server. It deletes resource identified by the Request-URI.
PATCH vs PUT
PUT method primarily fully replaces an entire existing resource but PATCH partially updates an existing resource.