HTTP Methods "GET", "POST", "PUT", "PATCH", "DELETE"

Bentil Shadrack - Aug 13 '22 - - Dev Community

To work with REST API efficiently, Its important to know the basic HTTP methods and what each of them does.

Out of 39 http methods, developers typically use the "GET", "POST", "PUT", "PATCH" and "DELETE" methods.
In this article, I will be showing you the difference between the 5 methods mentioned above and when to use them.

✔ GET

The GET method is used to retrieve resources from a server. It is said to be a safe method as it does not change the state of the resource in any way.
GET method is idempotent Thus calling this method multiple times will always give the same result.

Example URIs

HTTP GET 'http://www.apidomain.com/users'
HTTP GET 'http://www.apidomain.com/users?size=20&page=5'
HTTP GET 'http://www.apidomain.com/users/123'
HTTP GET 'http://www.apidomain.com/users/123/address'
Enter fullscreen mode Exit fullscreen mode

✔ 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.apidomain.com/users'
HTTP POST 'http://www.apidomain.com/users/123/accounts'
Enter fullscreen mode Exit fullscreen mode

✔ 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.apidomain.com/users/123'
HTTP PUT 'http://www.apidomain.com/users/123/accounts/456'
Enter fullscreen mode Exit fullscreen mode

✔ PATCH

PATCH is used to update the existing resource on the server and it updates a portion of the resource.
If the resource does not exist, PUT may decide to create a new resource.
Just as the PUT method, PATCH is also idempotent

Example URIs

HTTP PATCH 'http://www.apidomain.com/users/123'
HTTP PATCH 'http://www.apidomain.com/users/123/accounts/456'
Enter fullscreen mode Exit fullscreen mode

✨PATCH vrs PUT

PUT method primarily fully replaces an entire existing resource but PATCH partially updates an existing resource.

The PATCH method is not a substitute to the PUT method. It applies a delta (diff) rather than replacing the entire resource.

✔ DELETE

DELETE Method is used to delete the resources from a server. It deletes resource identified by the Request-URI.

DELETE method are idempotent.

Conclusion

As a backend developer, It is very important to follow the standard approach in building your API.

Bentil here🚀
If you like my content, you can support me here to keep up the work.👇

buy me a coffee

Let me know your questions or suggestions in the comment box below

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player