用于“由于*已知*原因而无法满足请求"的HTTP状态代码; [英] HTTP status code for "could not fulfill request for *known* reason"

查看:97
本文介绍了用于“由于*已知*原因而无法满足请求"的HTTP状态代码;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTTP 500表示服务器由于意外原因无法满足该请求.服务器由于已知预期的原因而无法满足请求时,使用的最佳HTTP响应代码是什么?

HTTP 500 means the server could not fulfill the request for an unexpected reason. What is the best HTTP response code to use when the server could not fulfill the request for a reason that is known or expected?

http://www.w3.org/Protocols/rfc2616/rfc2616- sec10.html https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

浏览HTTP上的一些文档,找不到很好的答案,这似乎是一个重要的区别.抛出500的错误并不能真正代表内部服务器错误"可能不是一个好习惯.

looking through some docs on HTTP, I can't find a good answer and it seems to be an important distinction. Throwing up a 500 for an error that doesn't really represent an "internal server error" is probably not a good practice.

推荐答案

不再使用RCF 2616作为参考

如今RFC 2616不再适用,任何使用此类文档作为参考的人都应立即停止.引用马克·诺丁汉,他在撰写本文时担任共同主席IETF HTTP和QUIC工作组:

Don't use the RCF 2616 as reference anymore

The RFC 2616 is no longer relevant nowadays and anyone using such document as reference should stop right away. Quoting Mark Nottingham who, at the time of writing, co-chairs the IETF HTTP and QUIC Working Groups:

请勿使用RFC2616 .从硬盘驱动器,书签中将其删除,并刻录(或负责地回收)打印出的所有副本.

Don’t use RFC2616. Delete it from your hard drives, bookmarks, and burn (or responsibly recycle) any copies that are printed out.

以下定义了HTTP/1.1协议的文档已取代了旧的RFC 2616:

The old RFC 2616 has been supplanted by the following documents that, together, define the HTTP/1.1 protocol:

  • RFC 7230: Message Syntax and Routing
  • RFC 7231: Semantics and Content
  • RFC 7232: Conditional Requests
  • RFC 7233: Range Requests
  • RFC 7234: Caching
  • RFC 7235: Authentication

如果您要查找状态码定义,则 RFC 7231 是您应使用的文档请参阅.

If you are looking for status code definitions, then the RFC 7231 is the document you should refer to.

根据已知或预期原因,您可以返回正确的状态代码:

Depending on the known or expected reason, you can return the proper status code:

  • 由于客户端请求的资源不存在而无法满足请求?返回 404 .
  • 这是授权问题吗?进入 403 .
  • 使用 HTTP身份验证,并且凭据无效吗?返回 401 .
  • 服务器不支持满足请求所需的功能吗?使用 501 .
  • 由于与目标资源的当前状态发生冲突而无法完成请求吗?因此,应该返回 409 .
  • 是否已为目标资源分配了新的永久URI?正确选择 301 状态代码.
  • 依此类推...
  • Couldn't the request be fulfilled because the client is requesting a resource that does not exist? Return a 404.
  • Is it an authorization problem? Go for 403.
  • Using HTTP authentication and the credentials are not valid? Return a 401.
  • Doesn't the server support the functionality required to fulfil the request? Use 501.
  • Couldn't the request be completed due to a conflict with the current state of the target resource? So 409 should be returned.
  • Has the target resource been assigned a new permanent URI? The 301 status code is the right choice.
  • And so on...

有关更多详细信息,请查看 RFC 7231 ,并查看以下决策表,其中

For more details, check the RFC 7231 and also have a look at the following decision chart that Michael Kropat put together:

状态代码分为三大类:

从这里开始:

这篇关于用于“由于*已知*原因而无法满足请求"的HTTP状态代码;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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