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'
โ 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'
โ 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'
โ 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'
โจ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.๐
Let me know your questions or suggestions in the comment box below
Top comments (4)
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?
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,
If your goal is for other developers to quickly understand and use your API, then use generally accepted practices that most developers expects.
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.
good for learning about rest verbs. i also like so.share me article about these verbs: dev.to/bias/restful-verbs-vs-real-...