SharpCoder
SharpCoder

Reputation: 19165

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

Upvotes: 11

Views: 24214

Answers (3)

Qamar Abbas
Qamar Abbas

Reputation: 21

GET use GET requests to retrieve resource representation/information only, and not modify it in any way.

POST use POST request to create a new subordinate resource, i.e. When talking strictly about REST, POST methods are used to create a new resource into the collection of resources, or a new row in the database.

PUT use PUT request primarily to update an existing resource (if the resource does not exist, then API may decide to create a new resource or not).

DELETE DELETE requests to delete the resources (identified by the Request-URI).

Upvotes: 1

shiraz27
shiraz27

Reputation: 2636

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

Upvotes: 3

ydaetskcoR
ydaetskcoR

Reputation: 56839

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.

Upvotes: 8

Related Questions