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

后端 未结 1 482
遥遥无期
遥遥无期 2021-01-19 12:51

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 wi

相关标签:
1条回答
  • 2021-01-19 13:15

    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.

    0 讨论(0)
提交回复
热议问题