什么时候使用POST?什么时候使用GET?

时间:2020-03-05 18:48:27  来源:igfitidea点击:

据我所知,共有三类:

  • 永远不要使用GETPOST
  • 切勿使用POSTGET
  • 使用哪一个都没关系。

我认为这三种情况正确吗?如果是这样,每种情况下有哪些示例?

解决方案

回答

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

回答

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

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

回答

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

回答

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

回答

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

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

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

回答

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

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

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

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

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

回答

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.

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

回答

简单来说

  • 使用GET处理安全和幂等的请求
  • 将POST用于安全或者幂等请求

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

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

"安全"操作是不"更改所请求的数据"的操作。

"幂等"运算是一种结果,无论我们请求多少次,结果都将"相同"。

可以合理地认为,由于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用于更改数据的操作。如果搜索引擎抓取了指向恶意操作的链接,或者客户添加了书签,则可能会带来很大的麻烦。

回答

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

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

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

回答

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

回答

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

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

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

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

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

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

回答

精简版

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.

回答

一个实际的区别是浏览器和Web服务器对URL中可以存在的字符数有限制。每种应用程序都有所不同,但是如果表单中有textarea,那么肯定可以将其选中。

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

回答

首先重要的是GET vs POST的含义:

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

之后,需要注意几件事:

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

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

回答

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

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

回答

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

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

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

回答

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

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

回答

一件重要的事情是,我们通过" GET"提交的所有内容都将通过URL公开。其次,正如Ceejayoz所说,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的典型示例是将反馈提交到在线表单。

回答

本质上,我们没有什么可以做的。关键是我们不应该在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.

回答

阅读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。

回答

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

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

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

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

在Web编程中,还有更多的解释空间。有一个应该做什么,一个可以做什么,但是哪个比较好通常会引起争议。幸运的是,在这种情况下,没有歧义。我们应该使用帖子来更改数据,并且应该使用get来检索信息。

回答

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