我正在设计一个API来通过HTTP,我想知道是否使用HTTP POST命令,但只有URL查询参数,没有请求体,是一个很好的方法。
注意事项:
“好的网页设计”要求通过POST发送非幂等动作。这是非幂等的。 当请求参数出现在URL中时,更容易开发和调试这个应用程序。 该API并不打算广泛使用。 似乎要做一个没有正文的POST请求需要更多的工作,例如必须显式添加Content-Length: 0报头。 在我看来,没有正文的POST有点违背大多数开发人员和HTTP框架的期望。
在POST请求中通过URL查询而不是通过请求体发送参数还有什么缺点或优点吗?
编辑:考虑这一点的原因是操作不是幂等的,除了检索之外还有其他副作用。请参阅HTTP规范:
In particular, the convention has been established that the GET and HEAD methods SHOULD NOT have the significance of taking an action other than retrieval. These methods ought to be considered "safe". This allows user agents to represent other methods, such as POST, PUT and DELETE, in a special way, so that the user is made aware of the fact that a possibly unsafe action is being requested. ... Methods can also have the property of "idempotence" in that (aside from error or expiration issues) the side-effects of N > 0 identical requests is the same as for a single request. The methods GET, HEAD, PUT and DELETE share this property. Also, the methods OPTIONS and TRACE SHOULD NOT have side effects, and so are inherently idempotent.