HTTP 406 和 415 错误代码

问题描述:

我正在编写一个只接受 json,也只输出 json 的 web 服务.

I am writing a web service that accepts only json, and also outputs only json.

因此,如果请求任何其他格式,我需要返回适当的状态代码.

So I need to return the appropriate status code if any other format is requested.

看来我有两个选择:

  1. 406 - 不可接受
  2. 415 - 不支持的媒体类型

如果有人能启发我了解这两个代码的语义,那就太好了.

It would be great if someone could enlighten me as to the semantics of the two codes.

406 由服务器返回,当它无法基于接受请求标头做出响应时(即,它们有一个 Accept 标头,说明它们only 想要 XML).

406 is returned by the server when it can't respond based on accepting the request headers (ie they have an Accept header which states they only want XML).

当请求中发送的实体(POST 或 PUT 中的内容)具有不受支持的媒体类型(即他们发送的 XML)时,服务器将返回 415.

415 is returned by the server when the entity sent in a request (content in a POST or PUT) has an unsupported mediatype (i.e. they sent XML).

所以.. 406 当你不能发送他们想要的东西时,415 当他们发送你不想要的东西时.

so.. 406 when you can't send what they want, 415 when they send what you don't want.

希望有帮助!