2012-11-26 83 views
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" 
        } 
       ] 
      } 
     } 
    ] 
} 

我可以用更好的數據作出反應?

回答

0

在這種情況下,我會說400 Bad Request是一個很好的響應代碼,因爲請求的格式不正確。您顯然可以在響應主體中提供有關驗證錯誤的更多詳細信息。

大多數其他4​​XX系列響應與用戶的身份驗證/授權問題有關,可能會找到資源或方法mot,或者請求標頭未正確發送。

在你的情況下,問題在於發送的數據,因此400可能是最好的響應代碼。

相關問題