HTTP 406和415错误代码

问题描述:

我正在编写一个只接受json的web服务,并且只输出json。

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标头,表明他们想要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).

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

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.

希望有所帮助!