关于http get:何时使用POST以及何时使用GET?

When do you use POST and when do you use GET?

据我所知,共有三类:

  • 请勿使用GETPOST
  • 切勿使用POSTGET
  • 不管使用哪个。
  • 我认为这三种情况正确吗? 如果是这样,每种情况下有哪些示例?


    使用POST进行破坏性操作,例如创建(我知道是讽刺的),编辑和删除,因为您无法在浏览器的地址栏中点击POST操作。在安全允许某人调用某个动作的情况下,请使用GET。像这样的URL:

    1
    http://myblog.org/admin/posts/delete/357

    应该带您到确认页面,而不是简单地删除项目。这样避免事故要容易得多。

    POST也比GET更安全,因为您没有将信息粘贴到URL中。因此,对于收集密码或其他敏感信息的HTML表单,将GET用作method并不是最好的主意。

    最后一点:POSTGET可以传输更多的信息。" POST"对传输的数据没有大小限制,而" GET"限制为2048个字符。


    简单来说

    • GET用于safe and idempotent请求
    • POST用于neither safe nor idempotent请求

    详细说明
    每个都有一个合适的地方。即使您不遵循RESTful原则,通过了解REST以及面向资源的方法如何工作,也可以获得很多好处。

    A RESTful application will use GETs for operations which are both safe and idempotent.

    safe操作是执行not change the data请求的操作。

    idempotent操作是无论请求多少次结果都将be the same的操作。

    可以肯定地说,由于GET用于安全操作,因此它们也自动成为幂等的。通常,GET用于检索资源(例如,关于堆栈溢出的问题及其相关答案)或资源集合。

    A RESTful app will use PUTs for operations which are not safe but idempotent.

    我知道问题是关于GET和POST的,但是我稍后将返回POST。

    通常,PUT用于编辑资源(例如,在堆栈溢出时编辑问题或答案)。

    A POST would be used for any operation which is neither safe or idempotent.

    通常,POST将用于创建新资源,例如创建NEW SO问题(尽管在某些设计中,PUT也将用于此)。

    如果您两次运行POST,最终将创建两个新问题。

    There's also a DELETE operation, but I'm guessing I can leave that there :)

    讨论区

    实际上,现代Web浏览器通常仅可靠地支持GET和POST(您可以通过javascript调用执行所有这些操作,但是就以表格形式输入数据并按Submit而言,通常会有两种选择)。在RESTful应用程序中,POST通常会被覆盖以提供PUT和DELETE调用。

    但是,即使您没有遵循RESTful原则,也可以考虑使用GET来检索/查看信息,以及使用POST来创建/编辑信息。

    绝对不要将GET用于更改数据的操作。如果搜索引擎抓取到您的恶意操作的链接,或者客户添加了书签,则可能会带来很大的麻烦。


    如果您不介意重复请求(即它不会更改状态),请使用GET。

    如果操作确实更改了系统状态,请使用POST。


    简洁版本

    GET:通常用于提交的搜索请求或您希望用户能够再次拉出确切页面的任何请求。

    GET的优点:

    • 可以安全地为URL添加书签。
    • 页面可以安全地重新加载。

    GET的缺点:

    • 变量作为名称/值对通过url传递。 (安全风险)
    • 可以传递的变量数量有限。 (基于浏览器。例如,Internet Explorer限制为2,048个字符。)

    POST:用于更高安全性的请求,在这些请求中,数据可能会用于更改数据库或您不希望某人添加书签的页面。

    POST的优点:

    • 名称/值对不显示在url中。 (安全性+ = 1)
    • 可以通过POST传递无限数量的名称/值对。参考。

    POST的缺点:

    • 使用POST数据的页面不能作为书签。 (如果您愿意)。

    较长的版本

    直接来自超文本传输??协议-HTTP / 1.1:

    9.3 GET

    The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI. If the Request-URI refers to a data-producing process, it is the produced data which shall be returned as the entity in the response and not the source text of the process, unless that text happens to be the output of the process.

    The semantics of the GET method change to a"conditional GET" if the request message includes an If-Modified-Since, If-Unmodified-Since, If-Match, If-None-Match, or If-Range header field. A conditional GET method requests that the entity be transferred only under the circumstances described by the conditional header field(s). The conditional GET method is intended to reduce unnecessary network usage by allowing cached entities to be refreshed without requiring multiple requests or transferring data already held by the client.

    The semantics of the GET method change to a"partial GET" if the request message includes a Range header field. A partial GET requests that only part of the entity be transferred, as described in section 14.35. The partial GET method is intended to reduce unnecessary network usage by allowing partially-retrieved entities to be completed without transferring data already held by the client.

    The response to a GET request is cacheable if and only if it meets the requirements for HTTP caching described in section 13.

    See section 15.1.3 for security considerations when used for forms.

    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 vs POST的含义:

    • GET应该用于...从服务器获取一些信息,
    • 而POST应该用于向服务器发送一些信息。

    之后,需要注意几件事:

    • 使用GET,您的用户可以使用其浏览器中的"后退"按钮,并且可以为页面添加书签
    • 可以作为GET传递的参数的大小有限制(某些版本的Internet Explorer,如果我没记错的话,为2KB); POST的限制更大,并且通常取决于服务器的配置。

    无论如何,如果没有GET,我认为我们无法"生存":考虑每天使用多少个带有查询字符串中参数的URL -如果没有GET,所有这些URL都将无法工作;-)


    除了在许多Web浏览器中的长度限制差异之外,还存在语义差异。 GET被认为是"安全的",因为它们是只读操作,不会更改服务器状态。 POST通常会更改状态,并会在重新提交时发出警告。搜索引擎的网络爬虫可能会生成GET,但绝不应发布POST。

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


    我的一般经验法则是在向服务器发出不会更改状态的请求时使用Get。保留帖子以供更改状态的服务器请求。


    一个实际的区别是浏览器和Web服务器对URL中可以存在的字符数有限制。每种应用程序都有所不同,但是如果您的表单中有textarea的话,当然可以打它。

    另一个与GET有关的陷阱-它们被搜索引擎和其他自动系统索引。 Google曾经有一种产品可以在您正在查看的页面上预取链接,因此,如果您单击这些链接,它们的加载速度会更快。它在具有delete.php?id=1之类的链接的网站上造成了严重破坏-人们失去了整个网站。


    当您希望URL反映页面状态时,请使用GET。这对于查看动态生成的页面(例如此处看到的页面)很有用。 POST应该以表格形式提交数据,例如当我单击" Post Your Answer"按钮时。由于它不会在路径后生成参数字符串,因此它还会生成一个更简洁的URL。


    因为GET纯粹是URL,所以它们可以被Web浏览器缓存,并且可以更好地用于诸如一致生成的图像之类的事情。 (设置到期时间)

    Gravatar页面上的一个示例:http://www.gravatar.com/avatar/4c3be63a4c2f539b013787725dfce802?d=monsterid

    GET可能会稍微提高性能,某些Web服务器在调用处理程序之前将POST内容写入临时文件。

    要考虑的另一件事是大小限制。 GET受URL大小限制,标准限制为1024字节,尽管浏览器可能支持更多。

    传输更多的数据应使用POST以获得更好的浏览器兼容性。

    正如另一个发布者所写,甚至比这个限制还小的问题,URL中的任何内容都可能会出现在浏览器用户界面的其他部分,例如历史记录。


    1.3用于选择HTTP GETPOST的快速清单

    在以下情况下使用GET:

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

    在以下情况下使用POST:

    1
    2
    3
        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.

    资源。


    这涉及到REST的概念以及如何使用Web。在Software Engineering广播中有一个出色的播客,深入讨论了Get和Post的用法。

    Get用于从服务器中提取数据,而无需执行更新操作。想法是,您应该能够反复使用相同的GET请求,并返回相同的信息。该URL在查询字符串中具有获取信息,因为该URL可以轻松地发送给其他系统和人,例如可以在哪里找到东西的地址。

    应该使用Post(至少是基于Web的REST架构)将信息推送到服务器/告诉服务器执行操作。例如:更新此数据,创建此记录。


    本质上,您无能为力。关键是您不应该在HTTP GET上修改服务器状态。 HTTP代理假定由于HTTP GET不会修改状态,因此用户一次调用HTTP GET还是1000次调用HTTP GET都没有区别。他们使用这些信息假设可以安全地返回第一个HTTP GET的缓存版本。如果违反了HTTP规范,则可能会冒用破坏HTTP客户端和代理的风险。不要这样做:)


    根据RFC 2616:

    9.3 GET
    The GET method means retrieve whatever information (in the form of
    an entity) is identified by the
    Request-URI. If the Request-URI refers
    to a data-producing process, it is the
    produced data which shall be returned
    as the entity in the response and not
    the source text of the process, unless
    that text happens to be the output of
    the process.

    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.


    POST可以移动大数据,而GET不能。

    但是通常,这不是关于GET的缺点,而是如果您希望自己的网站/ webapp表现良好,则是一种约定。

    看看http://www.w3.org/2001/tag/doc/whenToUseGet.html


    i dont see a problem using get though, i use it for simple things where it makes sense to keep things on the query string.

    使用它来更新状态(例如delete.php?id=5的GET来删除页面)是非常冒险的。人们发现,当Google的Web加速器开始预取页面上的URL时,它会点击所有"删除"链接并清除人们的数据。搜索引擎蜘蛛也会发生同样的事情。


    当我不希望人们看到QueryString或QueryString变大时,我会使用POST。另外,文件上传需要POST。

    我没有看到使用GET的问题,而是将它用于将事情保留在QueryString上的简单事情。

    使用GET也可以链接到特定页面,而POST也不起作用。


    从w3schools.com:

    What is HTTP?

    The Hypertext Transfer Protocol (HTTP) is designed to enable
    communications between clients and servers.

    HTTP works as a request-response protocol between a client and server.

    A web browser may be the client, and an application on a computer that
    hosts a web site may be the server.

    Example: A client (browser) submits an HTTP request to the server;
    then the server returns a response to the client. The response
    contains status information about the request and may also contain the
    requested content.

    Two HTTP Request Methods: GET and POST

    Two commonly used methods for a request-response between a client and
    server are: GET and POST.

    GET – Requests data from a specified resource POST – Submits data to
    be processed to a specified resource

    在这里,我们区分主要区别:

    enter image description here


    阅读Wikipedia中有关HTTP的文章。它将解释协议是什么以及它的作用:

    GET

    Requests a representation of the specified resource. Note that GET should not be used for operations that cause side-effects, such as using it for taking actions in web applications. One reason for this is that GET may be used arbitrarily by robots or crawlers, which should not need to consider the side effects that a request should cause.

    POST
    Submits data to be processed (e.g., from an HTML form) to the identified resource. The data is included in the body of the request. This may result in the creation of a new resource or the updates of existing resources or both.

    W3C有一个名为URI,可寻址性以及HTTP GET和POST用法的文档,该文档解释了何时使用什么。引用

    1.3 Quick Checklist for Choosing HTTP GET or POST

    • Use GET if:

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

    • Use POST if:

      • 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
        o The user be held accountable for the results of the interaction.

    However, before the final decision to use HTTP GET or POST, please also consider considerations for sensitive data and practical considerations.

    一个实用的示例是您提交HTML表单的任何时候。您可以为表单操作指定发布或获取。 PHP将相应地填充$ _GET和$ _POST。


    在PHP中,POST数据限制通常由您的php.ini设置。我相信GET受服务器/浏览器设置的限制-通常在255字节左右。


    最初的目的是使用GET来取回数据,而POST可以是任何东西。我使用的经验法则是,如果将任何内容发送回服务器,则使用POST。如果我只是调用URL来获取数据,则使用GET。


    POST GET PUT DELETE的简单版本

    • 使用GET-当您想要获取基于任何ID或名称的任何资源(如数据列表)时
    • 使用POST-要将任何数据发送到服务器时。请记住,POST是一项繁重的操作,因为要进行更新,我们应该使用PUT而不是POST
      内部POST将创建新资源
    • 使用PUT-当您

    另一个区别是POST通常需要两个HTTP操作,而GET仅需要一个HTTP操作。

    编辑:我应该澄清-常见的编程模式。通常,出于各种原因,使用简单的HTML网页响应POST是一个可疑的设计,其中一个令人讨厌的"您必须重新提交此表单,您希望这样做吗?"按下返回按钮。


    就像其他人回答的那样,get的URL大小是有限制的,并且文件只能通过post提交。

    我想补充一点,就是可以使用get将内容添加到数据库中,并使用post来执行操作。当脚本收到帖子或获取信息时,它可以执行作者希望执行的任何操作。我认为对本书的措辞或阅读方式缺乏理解。

    脚本作者应该使用帖子来更改数据库,并仅使用get来获取信息。

    脚本语言提供了许多访问请求的方法。例如,PHP允许使用$_REQUEST来检索帖子或获取。人们应该避免使用更具体的$_GET$_POST

    在Web编程中,还有更多的解释空间。有一个应该做什么,一个可以做什么,但是哪个更好呢?幸运的是,在这种情况下,没有歧义。您应该使用帖子来更改数据,并且应该使用get来检索信息。


    好一件主要的事情是,您通过GET提交的所有内容都将通过URL公开。其次,正如Ceejayoz所说,URL的字符数受到限制。


    Gorgapor,mod_rewrite仍经常使用GET。它仅允许将友好的URL转换为带有GET查询字符串的URL。


    HTTP Post数据没有指定的数据量限制,因为不同的浏览器对GET的限制也不同。 RFC 2068指出:

    Servers should be cautious about
    depending on URI lengths above 255
    bytes, because some older client or
    proxy implementations may not properly
    support these lengths

    具体来说,您应该针对它们的用途使用正确的HTTP构造。 HTTP GET应该没有副作用,可以通过HTTP代理等安全地刷新和存储。

    当您要针对url资源提交数据时,将使用HTTP POST。

    在搜索上使用HTTP GET的典型示例是Search?Query = my + query
    使用HTTP POST的典型示例是将反馈提交到在线表单。


    推荐阅读