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

允我心安 提交于 2019-12-11 02:50:02

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/24886013/http-verbs-when-to-use-get-post-put-delete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!