rest - 是否仍然有 HTTP 301 而不是 HTTP 308 的用例来指示资源已移动(特别是在 ReST 服务器中)?

标签 rest http http-status-codes

我正在指定一个 ReST 服务。我最近偶然发现了这个 IETF draft document这可以解释为建议 ReST 服务应始终返回 308。请注意,该文档似乎已过期而未被取代(我对 IETF 流程不是很清楚)。

我早些时候想到,服务应该为 GET 返回“301 Moved Permanently”,为 POST 返回“308 Permanent Redirect”,以确保它不会不恰本地转换为 GET(通过某些试图变得聪明的东西).

301 和 308 的使用似乎是用“应该”和“不应该”来表达的,而不是“必须”和“不能”,因此从哲学上讲,“正确的事情”是什么还不清楚。

GET 的 301 和 POST 的 308 应该有效。 GET 的 308 没有错误,因为 current revision在撰写本文时,包含响应 GET 生成 308 的示例。 现在有一个部署问题,如果客户端不理解 308,它应该将其视为 300(而不是 301,这对我来说更有意义),但 308 现在已被广泛理解,所以问题变成了你是否或为什么应该使用 HTTP 301?

如果 308 本质上是 301 的错误修复,为什么 301 没有在 HTTP/1.1 中正式弃用?是否存在将 POST 转换为 GET 以在 ReST 或更普遍的 HTTP 中重定向的合法用例?

对于 301 HTTP/1.1 说:

Note: For historical reasons, a user agent MAY change the request method from POST to GET for the subsequent request.

为什么这是个好主意?它在任何地方仍然有效吗?

另见:

最佳答案

If 308 is essentially a bug fix for 301, why isn't 301 officially deprecated in HTTP/1.1?

不,不是。

RFC 7538 ,它定义了 308 状态代码,不会使 301 失效或过时。它只是定义另一个 状态代码来填补 RFC 7231 上的空白,它没有定义永久 状态码 307 的变体:

+-------------------------------------------+-----------+-----------+
|                                           | Permanent | Temporary |
+-------------------------------------------+-----------+-----------+
| Allows changing the request method from   | 301       | 302       |
| POST to GET                               |           |           |
| Does not allow changing the request       | -         | 307       |
| method from POST to GET                   |           |           |
+-------------------------------------------+-----------+-----------+

实际上,我仍然看到 301 主要用于 SEO。我还看到一些 HTTP 客户端无法识别 308


除了RFC,让我们看看Mozilla 的MDN Web 文档say关于301:

The HyperText Transfer Protocol (HTTP) 301 Moved Permanently redirect status response code indicates that the resource requested has been definitively moved to the URL given by the Location headers. A browser redirects to this page and search engines update their links to the resource (in 'SEO-speak', it is said that the 'link-juice' is sent to the new URL).

Even if the specification requires the method (and the body) not to be altered when the redirection is performed, not all user-agents align with it - you can still find this type of bugged software out there. It is therefore recommended to use the 301 code only as a response for GET or HEAD methods and to use the 308 Permanent Redirect for POST methods instead, as the method change is explicitly prohibited with this status.

现在看看Mozilla says关于308:

The HyperText Transfer Protocol (HTTP) 308 Permanent Redirect redirect status response code indicates that the resource requested has been definitively moved to the URL given by the Location headers. A browser redirects to this page and search engines update their links to the resource (in 'SEO-speak', it is said that the 'link-juice' is sent to the new URL).

The request method and the body will not be altered, whereas 301 may incorrectly sometimes be changed to a GET method.

Note: Some Web applications may use the 308 Permanent Redirect in a non-standard way and for other purposes. For example, Google Drive uses a 308 Resume Incomplete response to indicate to the client when an incomplete upload stalled. (reference)

关于rest - 是否仍然有 HTTP 301 而不是 HTTP 308 的用例来指示资源已移动(特别是在 ReST 服务器中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53436107/

相关文章:

java - 如何创建 REST 兼容的 servlet?

java - 网络服务 : How do I detect the endpoint that sent the message

javascript - 在 Angular JS 中使用工厂和 http

node.js - 如何在没有套接字的情况下及时发送多个HTTP响应(报告服务器处理状态)

php - 使用 AngularJS 将数组作为参数传递给 PHP Restful Api

javascript - 在 jquery.form.js 中捕获状态代码

rest - 404 状态代码是否适用于插入时不存在的实体?

java - 确定此 REST 应用程序背后的工具?

http - IIS7 和 HTTP 状态码处理

python - django rest 框架中的自定义 throttle 响应