据我所知,有三种类型:

不要使用GET和POST 不要使用POST而使用GET 你用哪一个都不重要。

我对这三种情况的假设正确吗?如果有,每个案例都有哪些例子?


当前回答

简单版本的POST GET PUT DELETE

使用GET -当你想获得任何资源,如基于任何Id或名称的数据列表 使用POST -当你想发送任何数据到服务器。请记住POST是一个重量级操作,因为对于更新,我们应该使用PUT而不是POST POST将创建新的资源 使用PUT -当你

其他回答

当您希望URL反映页面的状态时,请使用GET。这对于查看动态生成的页面非常有用,比如这里看到的页面。POST应该在表单中用来提交数据,就像我点击“POST Your Answer”按钮一样。它还生成了一个更干净的URL,因为它没有在路径后生成参数字符串。

1.3 HTTP GET / POST选择快速检查

在以下情况下使用GET:

    The interaction is more like a question (i.e., it is a safe operation such as a query, read operation, or lookup).

在以下情况下使用POST:

    The interaction is more like an order, or
    The interaction changes the state of the resource in a way that the user would perceive (e.g., a subscription to a service), or
    The user be held accountable for the results of the interaction.

源。

我不认为使用get有什么问题,我用它来做一些简单的事情,在查询字符串上保留东西是有意义的。

使用它来更新状态-像delete.php的GET ?Id =5删除一个页面是非常危险的。人们发现,当谷歌的网络加速器开始在页面上预取url时,它会击中所有的“删除”链接,并清除人们的数据。同样的事情也会发生在搜索引擎蜘蛛身上。

来自RFC 2616:

9.3获得 GET方法意味着检索任何信息(形式为 实体)由 要求通用。如果Request-URI引用 对于数据生成过程,它是 应返回的已生成数据 作为响应中的实体而不是 进程的源文本,除非 这个文本恰好是的输出 这个过程。

9.5 POST The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line. POST is designed to allow a uniform method to cover the following functions: Annotation of existing resources; Posting a message to a bulletin board, newsgroup, mailing list, or similar group of articles; Providing a block of data, such as the result of submitting a form, to a data-handling process; Extending a database through an append operation. The actual function performed by the POST method is determined by the server and is usually dependent on the Request-URI. The posted entity is subordinate to that URI in the same way that a file is subordinate to a directory containing it, a news article is subordinate to a newsgroup to which it is posted, or a record is subordinate to a database. The action performed by the POST method might not result in a resource that can be identified by a URI. In this case, either 200 (OK) or 204 (No Content) is the appropriate response status, depending on whether or not the response includes an entity that describes the result.

除了许多浏览器的长度限制不同之外,还有语义上的差异。get应该是“安全的”,因为它们是只读操作,不会改变服务器状态。post通常会改变状态,并在重新提交时给出警告。搜索引擎的网络爬虫可能会生成get,但不应该生成post。

如果希望读取数据而不改变状态,则使用GET;如果希望更新服务器上的状态,则使用POST。