我的RESTFul API只能响应JSON编码的数据(即我的所有标题都有Content-Type: application/json
).如果请求的Accept
标头不允许JSON(例如Accept: text/html
),我应该返回什么?我应该只返回一个400 Bad Request
带有解释的正文,还是有一个更具体的状态代码用于此异常?
请注意,这与不受支持的请求内容类型不同.
Kyl*_*lar 10
如果你想在语义上正确:
如果请求是HTTP/1.0:
a 406 Not Acceptable是正确的返回,因为客户端可能无法处理不是所请求类型的响应.
在HTTP/1.1中,这仍然是"正确"的事情,但也有例外,
来自RFC 2616 Sec 10.4.7
Note: HTTP/1.1 servers are allowed to return responses which are
not acceptable according to the accept headers sent in the
request. In some cases, this may even be preferable to sending a
406 response. User agents are encouraged to inspect the headers of
an incoming response to determine if it is acceptable.
Run Code Online (Sandbox Code Playgroud)
事实上,正如@Jack所提到的那样,它的可能性非常低.为了完整性,我只包括这个答案.
归档时间: |
|
查看次数: |
4234 次 |
最近记录: |