1
我有一個REST風格的web服務來編寫代碼,現在已經到了必須驗證發送到服務器的請求日期,同時執行帶有多個實體的PUT方法的時間點。如果在RESTful web服務中驗證提交的數據失敗,該如何迴應?
但現在最大的問題是:我該如何迴應(也許多)驗證錯誤?
請求的方法和URL:
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"
}
]
}
}
]
}
我可以用更好的數據作出反應?