使用 POST 方法调用 REST api 会调用 GET 方法
我正在开发一个 firefox 插件,它使用 post 方法调用rest api。如果使用 POST 调用,其余 api 将保留数据。如果使用 GET 方法调用它,它将检索之前保存的数据。
但在某些情况下,即使使用 POST 数据调用,API 也会返回持久数据。
我很困惑是我发送数据的方式有问题还是api有问题。但大多数请求都得到了正确的处理。所以,我不认为插件有什么问题。后端位于 ROR 上。
任何帮助都会很棒!
谢谢。
I am developing a firefox plugin which invokes a rest api using post method. The rest api will persist the data if it is invoked using POST. If it is invoked using GET method, it will retrieve the data persisted previously.
But in some cases, the api returns the persisted data even though it is invoked using the POST data.
I am under confusion whether the way I send the data is wrong or whether there is something wrong with api. But most of the requests gets served correctly. So, I don't think that there is something wrong with plugin. Te back end is on ROR.
Any help will be great!
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
RESTful 服务返回(更新的)数据副本以响应 POST 的情况并不罕见。
您真正关心的是它在您发布时更新远程数据。您可以安全地忽略它是否返回该数据的副本。
It's not uncommon for RESTful services to return a copy of the (updated) data in response to a POST.
All you really care about is that it's updating the remote data when you POST. Whether or not it returns a copy of that data is something you may be able to safely ignore.
如果它是间歇性的,一种可能是服务器和客户端之间存在 HTTP 缓存,该缓存实现得很差,并且无法正确区分 GET 和 POST 方法。也许它正在尝试返回缓存的副本,就像您发出了 GET 请求一样,即使您发送了 POST 请求。
相反,如果“在某些情况下”意味着“对于某些 URL”,那么您可能会 POST 到“/foo”(无尾部斜杠)之类的 URL,并且服务会返回 301 或 302,从而将客户端重定向到“/” foo/"(带有尾部斜杠)。大多数用户代理会将 302 的 POST 更改为 GET,有些会将 301 更改为 GET。如果您可以控制服务器,则应该使用 303 或 307。请参阅 https://datatracker .ietf.org/doc/html/draft-ietf-httpbis-p2-semantics-16#section-8.3.3
If it's intermittent, one possibility is that there is an HTTP cache in between your server and client which is poorly implemented, and doesn't differentiate properly between the GET and POST methods. Perhaps it is attempting to return a cached copy as if you had made a GET request even though you sent a POST request.
If instead "in some cases" means "for some URL's", then it's possible that you are POST'ing to a URL like "/foo" (no trailing slash) and the service returns 301 or 302, redirecting the client to "/foo/" (with a trailing slash). Most user-agents will change the POST to a GET for 302, and some will for 301. If you have control over the server, you should use 303 or 307 instead. See https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-p2-semantics-16#section-8.3.3