Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP verbs - When to use GET/POST/PUT/Delete

When you work on RESTFUL service you often hear the terms GET/POST/PUT/DELETE. My question is what is the idea behind so many verbs? I can achieve everything with the help of GET verb or if I want to post some large data in the body of the message, I can use POST verb. I do not think there is a need to think beyond these two verbs.

Do we have any general guideline in terms of when to use which verb? Is there any advantage of using one verb over the other??

PS: I know the idea behind

GET : Get object
PUT : Modify Object
DELETE: Delete Object
POST : Create Object
like image 844
SharpCoder Avatar asked Jul 22 '14 11:07

SharpCoder


People also ask

When to use GET POST and PUT?

The PUT Method The difference between POST and PUT is that PUT requests are idempotent. That is, calling the same PUT request multiple times will always produce the same result. In contrast, calling a POST request repeatedly have side effects of creating the same resource multiple times.

What is difference between HTTP methods get POST put and delete?

The POST method submits an entity to the specified resource, often causing a change in state or side effects on the server. The PUT method replaces all current representations of the target resource with the request payload. The DELETE method deletes the specified resource.

What are the 4 types of HTTP request methods?

The most commonly used HTTP request methods are GET, POST, PUT, PATCH, and DELETE. These are equivalent to the CRUD operations (create, read, update, and delete).


2 Answers

Each of the verbs serve different purposes. While it is possible to simply parse the body and ignore the request method this is very bad practice and makes it harder for anyone to better understand your web service.

Wikipedia summarises the request methods and their expected behaviours.

In general:

  • A GET should be used for requesting information from the web service.

  • A POST should be used to put data to a web server, where there is no specification as to where the web service should put the data. An example might be a question on StackOverflow. This may be considered the equivalent of an insert.

  • A PUT should be used when you want to specify where the data goes. This is an idempotent action as repeating it will not change anything on each repeated call. An example might be an answer or comment on StackOverflow as they would be linked to a resource such as being the answer to a specific question. Alternatively this may be considered as the equivalent of an update.

  • And a DELETE is obviously to be used to delete some data or a resource from the web server.

There are other request methods (as mentioned in the Wikipedia article) but these cover the main interactions that people will have with a web service.

like image 101
ydaetskcoR Avatar answered Sep 28 '22 18:09

ydaetskcoR


from the official mozilla developers website

  • GET The GET method requests a representation of the specified resource. Requests using GET should only retrieve data.

  • POST The POST method submits an entity to the specified resource, often causing a change in state or side effects on the server.

  • PUT The PUT method replaces all current representations of the target resource with the request payload.

  • DELETE The DELETE method deletes the specified resource.

for more check official docs

like image 20
shiraz27 Avatar answered Sep 28 '22 17:09

shiraz27