1

我有一个 RESTful Web 服务来编写代码,现在我必须验证发送到服务器的请求日期,同时使用多个实体执行 PUT 方法。

但现在最大的问题是:我如何应对(可能很多)验证错误?

请求方法和 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"
                    }
                ]
            }
        }
    ]
}

我可以用更好的数据来回应吗?

4

1 回答 1

0

400 Bad Request在这种情况下,我会说 a将是一个很好的响应代码,因为请求的格式不正确。您显然可以在响应正文中提供有关验证错误的更多详细信息。

大多数其他 4XX 系列响应与用户的身份验证/授权问题、找不到资源或方法或未正确发送请求标头有关。

在您的情况下,问题在于发送的数据,因此 400 可能是最好的响应代码。

于 2012-11-26T18:04:31.053 回答