请求正文中出现错误时的 HTTP 状态代码
HTTP status codes when errors in the request body
有人可以阐明在以下情况下我应该期待什么状态代码吗?例如,我发送 POST 请求,正文格式为:
{
"id": 321,
"username": "tombrown",
"email": "tombrown@gmail.com",
"password": "qwerty123",
"activated": true
}
所以问题是:
1) 如果我指定的数据类型错误,服务器是否应该return 400,例如,"id":“threetwoone”而不是int,"activated":“yes”布尔值等。或者服务器应该在这里 return 422?
2)“id”值应该是int,但实际上是long int,例如9223372036854774700.
3) 正文中缺少某些字段,例如我尝试发送:
{
"id": 321,
"username": "tombrown",
"activated": true
}
这些示例应该导致 400、422 还是其他一些选项?什么反应应该是正确的?
如果JSON语法无效,return400
. If JSON is syntactically valid but its content is invalid, return 422
表示服务器无法处理请求实体。
请参阅 RFC 4918 中的以下引用(对于您的情况,只需阅读 JSON 中的 XML):
11.2. 422 Unprocessable Entity
The 422
(Unprocessable Entity) status code means the server
understands the content type of the request entity (hence a
415
(Unsupported Media Type) status code is inappropriate), and the
syntax of the request entity is correct (thus a 400
(Bad Request)
status code is inappropriate) but was unable to process the contained
instructions. For example, this error condition may occur if an XML
request body contains well-formed (i.e., syntactically correct), but
semantically erroneous, XML instructions.
有人可以阐明在以下情况下我应该期待什么状态代码吗?例如,我发送 POST 请求,正文格式为:
{
"id": 321,
"username": "tombrown",
"email": "tombrown@gmail.com",
"password": "qwerty123",
"activated": true
}
所以问题是:
1) 如果我指定的数据类型错误,服务器是否应该return 400,例如,"id":“threetwoone”而不是int,"activated":“yes”布尔值等。或者服务器应该在这里 return 422?
2)“id”值应该是int,但实际上是long int,例如9223372036854774700.
3) 正文中缺少某些字段,例如我尝试发送:
{
"id": 321,
"username": "tombrown",
"activated": true
}
这些示例应该导致 400、422 还是其他一些选项?什么反应应该是正确的?
如果JSON语法无效,return400
. If JSON is syntactically valid but its content is invalid, return 422
表示服务器无法处理请求实体。
请参阅 RFC 4918 中的以下引用(对于您的情况,只需阅读 JSON 中的 XML):
11.2. 422 Unprocessable Entity
The
422
(Unprocessable Entity) status code means the server understands the content type of the request entity (hence a415
(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a400
(Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions.