我正在编写一个仅接受json的Web服务,并且只输出json。
所以我需要返回适当的状态码,如果请求了其他任何格式。
看来我有两个选择:
406 - 不可接受 415 - 不支持的介质类型如果有人能够启发我关于两个代码的语义,这将是巨大的。
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.
It appears that I have two choices:
406 - Not Acceptable 415 - Unsupported Media TypeIt would be great if someone could enlighten me as to the semantics of the two codes.
最满意答案
406由服务器返回,当它不能基于接受请求头(即它们有一个Accept标头,它们声明他们只想要XML)进行响应时。
当请求中发送的实体(POST或PUT中的内容)具有不受支持的媒体类型(即,它们发送XML)时,服务器返回415。
所以.. 406当你不能发送他们想要什么,415当他们发送你不想要的。
希望有帮助!
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 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).
so.. 406 when you can't send what they want, 415 when they send what you don't want.
Hope that helps!
更多推荐
发布评论