HTTP 406和415错误代码 [英] HTTP 406 and 415 error codes

查看:598
本文介绍了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.

希望有所帮助!

这篇关于HTTP 406和415错误代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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