我有一个RESTful webservice代码,现在我必须验证发送到服务器的请求日期,同时执行带有多个实体的PUT-Method。
但现在最大的问题是:我如何回应(可能很多)验证错误?
请求方法和网址:
PUT http://example.dev/app_dev.php/api-1.0/labels.xml
请求正文(服务器收到它):
array(size=2)
0 =>
array (size=11)
'id' => string '53' (length=2)
'name' => string '2222' (length=4)
'app_domain' => string '11' (length=2)
[...]
1 =>
array (size=12)
'id' => string '54' (length=2)
'name' => string 'testname2' (length=9)
'controllpanel_domain' => string 'testname2' (length=9)
'label' => string 'testname2' (length=9)
'app_domain' => string 'testname2' (length=9)
[...]
我目前最终得到以下回复。 HTTP状态代码为400。
xml中的:
<?xml version="1.0" encoding="UTF-8"?>
<result>
<status>
<![CDATA[error]]>
</status>
<code>400</code>
<text>
<![CDATA[Bad Request]]>
</text>
<message>
<![CDATA[Unable to validate label entities]]>
</message>
<violations>
<object>
<object>
<label>
<entry>
<violation_message>
<![CDATA[This value should not be blank]]>
</violation_message>
</entry>
</label>
<controllpanel_domain>
<entry>
<violation_message>
<![CDATA[This value should not be blank]]>
</violation_message>
</entry>
</controllpanel_domain>
<app_domain>
<entry>
<violation_message>
<![CDATA[This value is too short. It should have 3 characters or more]]>
</violation_message>
</entry>
<entry>
<violation_message>
<![CDATA[This value should be 333 or more]]>
</violation_message>
</entry>
</app_domain>
</object>
</object>
<object>
<object>
<controllpanel_domain>
<entry>
<violation_message>
<![CDATA[This value should be a valid number]]>
</violation_message>
</entry>
</controllpanel_domain>
<app_domain>
<entry>
<violation_message>
<![CDATA[This value should be a valid number]]>
</violation_message>
</entry>
</app_domain>
<login_left_text>
<entry>
<violation_message>
<![CDATA[This value should not be blank]]>
</violation_message>
</entry>
</login_left_text>
</object>
</object>
</violations>
</result>
在json:
{
"status": "error",
"code": 400,
"text": "Bad Request",
"message": "Unable to validate label entities",
"violations": [
{
"object": {
"label": [
{
"violation_message": "This value should not be blank"
}
],
"controllpanel_domain": [
{
"violation_message": "This value should not be blank"
}
],
"app_domain": [
{
"violation_message": "This value is too short. It should have 3 characters or more"
},
{
"violation_message": "This value should be 333 or more"
}
]
}
},
{
"object": {
"controllpanel_domain": [
{
"violation_message": "This value should be a valid number"
}
],
"app_domain": [
{
"violation_message": "This value should be a valid number"
}
],
"login_left_text": [
{
"violation_message": "This value should not be blank"
}
]
}
}
]
}
我可以回复更好的数据吗?
答案 0 :(得分:0)
我想说400 Bad Request
在这种情况下是一个很好的响应代码,因为请求的格式不正确。显然,您可以在响应正文中提供有关验证错误的更多详细信息。
大多数其他4XX系列响应都与用户身份验证/授权问题有关,找不到资源或方法,或者请求标头未正确发送。
在您的情况下,问题在于发送的数据,因此400可能是最佳响应代码。