HTTP动词 - 何时使用GET / POST / PUT / Delete [英] HTTP verbs - When to use GET/POST/PUT/Delete

查看:784
本文介绍了HTTP动词 - 何时使用GET / POST / PUT / Delete的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您使用RESTFUL服务时,您经常会听到 GET / POST / PUT / DELETE 这些条款。我的问题是这么多动词背后的想法是什么?我可以在 GET 动词的帮助下实现一切,或者如果我想在消息正文中发布一些大数据,我可以使用 POST 动词。我不认为有必要超越这两个动词。



对于何时使用哪个动词,我们有任何一般指导原则吗?
使用一个动词比另一个动词有什么优势?



PS:我知道背后的想法

  GET:获取对象
PUT:修改对象
DELETE:删除对象
POST:创建对象


解决方案

每个动词都有不同的用途。虽然可以简单地解析正文并忽略请求方法,但这是非常糟糕的做法,并且使任何人都更难以更好地理解您的Web服务。

维基百科总结了请求方法及其预期行为。



一般来说:


  • 应该使用 GET 从网上
    服务请求信息。

  • 应该使用 POST 来放置数据到Web服务器,其中
    没有规定Web服务应该放置数据的位置。
    示例可能是StackOverflow的一个问题。这可能被认为是相当于一个插入。


  • 如果您想要使用A PUT 指定数据的位置。
    这是一个幂等动作,重复它不会改变每次重复呼叫的任何
    。一个例子可能是对
    StackOverflow的回答或评论,因为它们会链接到某个资源,例如某个特定问题的
    答案。另外,这可能会被认为是等同于更新。


  • 显然, DELETE 用于从Web服务器中删除一些数据或
    资源。


    还有其他请求方法(正如维基百科文章中提到的那样),但是这些涵盖了人们对Web服务的主要交互。


    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
    

    解决方案

    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.

    这篇关于HTTP动词 - 何时使用GET / POST / PUT / Delete的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆