HTTP GET 和 POST 参数建议 [英] HTTP GET and POST parameters recommendations

查看:27
本文介绍了HTTP GET 和 POST 参数建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发出以下 POST 请求是不好的做法吗:

Is it bad practice to issue the following POST request:

/test?a=1&b=2
POST data: c=3&d=4

请注意,2 个参数是 URL 的一部分,2 个参数是 POST 内容的一部分.

Notice that 2 parameters are part of the URL and 2 parameters are part of the POST content.

另一方面,是否仍然推荐以下规则:

On another note, is the following rule still recommended:

  • GET 请求:从服务器,但不要改变任何东西在服务器上.
  • POST 请求:post内容到服务器,这可能修改服务器上的数据

我之所以这么问是因为我在网上看到了一些东西.

I am asking because I see a bit of everything online.

洛朗·卢斯

推荐答案

是的,您的假设是正确的.您应该在如何传递参数或要求传递参数上保持一致,但这实际上不会造成任何伤害.

Yes, your assumptions are correct. You should be consistent on how you pass your parameters or require the parameters to be passed, but it's not going to do any harm really.

GET 操作应该是安全的操作,不会执行任何副作用(除了缓存等),因此它们很容易被代理等缓存.另一方面,POST 操作可能会产生副作用.

GET operations are supposed to be safe operations, that don't perform any side-effects (besides caching, etc), so they are easily cached by proxies and such. POST operations on the other hand may encure side effects.

我建议阅读 维基百科关于 HTTP 协议的条目:

获取

请求指定资源的表示.请注意,GET 不应用于导致副作用的操作,例如将其用于在 Web 应用程序中执行操作.一个原因是 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. See safe methods below.

发布

将要处理的数据(例如,从 HTML 表单)提交到标识的资源.数据包含在请求正文中.这可能会导致创建新资源或更新现有资源或两者兼而有之.

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.

还有其他操作(例如 HEAD、PUT、DELETE),如果您正在设计 API,您应该考虑使用它们.这些在 RESTful API 设计中进行了大量讨论.

There are other operations too (e.g. HEAD, PUT, DELETE), and you should consider using them if you are designing an API. These are heavily discussed in RESTful API design.

这篇关于HTTP GET 和 POST 参数建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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