DEV Community

Bentil Shadrack
Bentil Shadrack

Posted on • Edited on

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

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

Top comments (4)

Collapse
 
andruraj profile image
andruraj

Bro, I have a doubt. Why can't we use a single method? For example, I can have a single POST method, inside it, based on some input value, i can perform CRUD right? Is there any issue in it? Or any advantage in using GET, PUT, DELETE, PATCH explicitly?

Collapse
 
qbentil profile image
Bentil Shadrack • Edited

Aside the fact that, using the right HTTP request method makes it easier for other developers to follow and understand your API, Each of the method in one way or the other differs and have their own protocols.

For instance,

PUT requests are idempotent, meaning that executing the same PUT request will always produce the same result.
On the other hand, a POST will produce different outcomes. If you execute a POST request multiple times, you'll create a new resource multiple times despite them having the same data being passed in.

If your goal is for other developers to quickly understand and use your API, then use generally accepted practices that most developers expects.

Collapse
 
hamidmolareza profile image
Hamid Molareza

Why don't you write all your codes in one file and one function?!

Apart from the fact that there is a significant difference between GET and POST, code cleanliness and structure are always important.

Collapse
 
bias profile image
Tobias Nickel

good for learning about rest verbs. i also like so.share me article about these verbs: dev.to/bias/restful-verbs-vs-real-...