REST API的客户端应如何在POST时处理http状态301重定向? [英] How should client of Restful API handle http status 301 redirect upon POST?

查看:521
本文介绍了REST API的客户端应如何在POST时处理http状态301重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

经过精心设计的应用程序应如何处理301永久移动,并在HTTP POST上重定向到静态api?

How should a well-designed application handle 301 "moved permanently" redirects upon http POST to restful api?

上下文


  • 我们的托管应用程序提供了一个宁静的api

  • 我们有一个广泛使用其api的客户在他们自己的本地应用程序中[fwiw,已安装在许多站点上,没有简便的更新方法]

  • 我们正在将应用程序迁移到新的数据中心,在此过程中,我们会将它们(以及其他)从纯文本http切换为 ssl加密的https。 [物理位置数据中心固然无关紧要,但是新的数据中心具有更严格的安全规则,因此要求使用https]。

  • Our hosted application offers a restful api
  • We have a customer extensively who uses our restful api in their own 'on-premise' application [fwiw, installed at dozens of sites-- with no easy way to update]
  • We are migrating our application to a new data centers and in the process we will switch them (and others) from 'plain text http' to 'ssl-encrypted https'. [The physical location data center of course is irrelevant, but the new data center has more stringent security rules and thus mandates https]

我们的前端(haproxy / nginx)将发送301'永久移动'

Our front end (haproxy/nginx) will send a 301 'Moved Permanently'

我知道宁静的api应该是永久性的,但是 merde到达(法语中是 shit发生)-帝国崩溃了,柏林墙倒塌,甲骨文收购了Sun等。

I know that "restful api's should be permanent", but, merde arrive (that's shit happens in French)--Empires crumble, Berlin Wall falls, Oracle buys Sun etc.

问题

他们的应用程序使HTTP发布调用到我们宁静的api。当前端返回 http status 301时,他们的应用程序不会重新发布到新的url,并且更新失败。

Their application makes HTTP post calls to our restful api. When the front end returns 'http status 301', their application does not 're-post' to the new url, and the update fails.

问题


  • 错误处理应如何处理301和其他http状态? (伪代码就足够了)

  • 我们的前端应该为 301做些不同的事情吗?

推荐答案

要满足纯RESTfulness意义上的精心设计的要求,客户端应将请求重新发送到新URI。响应代码301表示资源已移动,无法用于完成请求,因此实际上没有后备位置。

To fulfill your "well-designed" requirement in the sense of "pure RESTfulness", the client should re-send the request to the new URI. A response code of 301 indicate the resource has moved, and cannot be used to fulfill the request, so there's really no fallback position.

如果客户端尝试重新发布,但会丢失数据,这是一个客户端错误。客户端的正确行为因您的要求而异:它可以将重定向视为可恢复的错误情况,并透明地重新发布;它可以在指示用户更新端点时重新发布;否则它可能会失败,并显示相应的错误消息。

If the client attempts to re-post, but loses data, that's a client bug. The "correct" behavior of a client varies by your requirements: it could treat the redirect as a recoverable error case, and repost transparently; it could repost while instructing the user to update the endpoint; or it could fail with an appropriate error message.

这篇关于REST API的客户端应如何在POST时处理http状态301重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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