为什么使用HTTP GET在RESTful调用中更新服务器上的状态不正确? [英] Why is using a HTTP GET to update state on the server in a RESTful call incorrect?

查看:141
本文介绍了为什么使用HTTP GET在RESTful调用中更新服务器上的状态不正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我知道所有原因 在纸上 为什么我不应 在进行RESTful调用时更新服务器上的某种状态。因此每次返回可能不同的数据。我知道这是错误的,因为下面的'论文'原因:

OK, I know already all the reasons on paper why I should not use a HTTP GET when making a RESTful call to update the state of something on the server. Thus returning possibly different data each time. And I know this is wrong for the following 'on paper' reasons:


  • HTTP GET调用应该是幂等的

  • N> 0调用应始终获取相同的数据返回 违反HTTP规范

  • HTTP GET调用通常为只读
  • li>
  • HTTP GET calls should be idempotent
  • N > 0 calls should always GET the same data back
  • Violates HTTP spec
  • HTTP GET call is typically read-only

我相信还有更多的理由。但是我需要一个具体的简单例子来说明理由,而不是违反HTTP Spec!。 ...或者至少我希望有一个。我也已经阅读了以上列表中更多的内容:&
使用网址查询参数的HTTP POST - 好主意还是不好?

And I am sure there are more reasons. But I need a concrete simple example for justification other than "Well, that violates the HTTP Spec!". ...or at least I am hoping for one. I have also already read the following which are more along the lines of the list above: Does it violate the RESTful when I write stuff to the server on a GET call? & HTTP POST with URL query parameters -- good idea or not?

例如,有人可以证明上述理由,以及为什么它是错误的/错误的做法/不正确的使用HTTP GET说与在RESTful调用之后

For example, can someone justify the above and why it is wrong/bad practice/incorrect to use a HTTP GET say with the following RESTful call

"MyRESTService/GetCurrentRecords?UpdateRecordID=5&AddToTotalAmount=10"

我知道这是错误的,但希望它有助于提供一个例子来回答我的原始问题。因此,上面的代码将使用AddToTotalAmount = 10更新recordID = 5,然后返回更新的记录。我知道一个POST 应该被使用,但是让我们说我确实使用了GET。

I know it's wrong, but hopefully it will help provide an example to answer my original question. So the above would update recordID = 5 with AddToTotalAmount = 10 and then return the updated records. I know a POST should be used, but let's say I did use a GET.

和回答我的问题是否会导致实际的问题?除了上面列表中的所有违规行为之外,如何使用HTTP GET来完成上述操作会导致一些实际问题?太多次我进入一个场景,我可以用因为文档如此说来证明事情的合理性,但是我需要对此进行理由和更好的理解。

How exactly and to answer my question does or can this cause an actual problem? Other than all the violations from the above bullet list, how can using a HTTP GET to do the above cause some real issue? Too many times I come into a scenario where I can justify things with "Because the doc said so", but I need justification and a better understanding on this one.

谢谢!

推荐答案

您遇到问题的实际情况是,HTTP GET通常在发生故障时重试HTTP实现。因此,您可以在现实生活中获得服务器多次收到相同GET的情况。如果你的更新是幂等的(你的是),那么就没有问题了,但是如果它不是幂等的(比如像增加一些值),那么你可能会得到多个(不需要的)更新。

The practical case where you will have a problem is that the HTTP GET is often retried in the event of a failure by the HTTP implementation. So you can in real life get situations where the same GET is received multiple times by the server. If your update is idempotent (which yours is), then there will be no problem, but if it's not idempotent (like adding some value to an amount for example), then you could get multiple (undesired) updates.

HTTP POST永远不会重试,所以您永远不会遇到这个问题。

HTTP POST is never retried, so you would never have this problem.

这篇关于为什么使用HTTP GET在RESTful调用中更新服务器上的状态不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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