ApiGility - 通过PUT获取验证错误

时间:2015-01-15 12:55:30

标签: zend-framework zend-framework2 apigility

我是ApiGility的新用户,我正在尝试通过API调用更新我的购物车,因为它与路线相关。我正在使用Zend Framework 2和代码连接api。

我目前面临的问题是,无论我尝试什么,我都无法在没有验证错误的情况下将信息发送到api。

我的module.config:

Updatecart设置:

 'Api\\V1\\Rest\\Updatecart\\Controller' => array(
        'listener' => 'Api\\V1\\Rest\\Updatecart\\UpdatecartResource',
        'route_name' => 'api.rest.updatecart',
        'route_identifier_name' => 'updatecart_id',
        'collection_name' => 'updatecart',
        'entity_http_methods' => array(
            0 => 'GET',
            1 => 'PATCH',
            2 => 'PUT',
            3 => 'DELETE',
        ),
        'collection_http_methods' => array(
            0 => 'GET',
            1 => 'POST',
            2 => 'PUT',
            3 => 'PATCH',
            4 => 'DELETE',
        ),
        'collection_query_whitelist' => array(
            0 => 'prod_id',
            1 => 'quantity',
            2 => 'quantity_accumulation',
            3 => 'tax',
        ),
        'page_size' => 25,
        'page_size_param' => null,
        'entity_class' => 'Api\\V1\\Rest\\Updatecart\\UpdatecartEntity',
        'collection_class' => 'Api\\V1\\Rest\\Updatecart\\UpdatecartCollection',
        'service_name' => 'updatecart',
    ),

相关过滤器设置:

'Api\\V1\\Rest\\Updatecart\\Validator' => array(
        0 => array(
            'name' => 'prod_id',
            'required' => true,
            'filters' => array(
                0 => array(
                    'name' => 'Zend\\Filter\\Int',
                    'options' => array(),
                ),
            ),
            'validators' => array(),
            'description' => 'The id of the product in the cart to be updated',
            'error_message' => 'Missing product id',
            'allow_empty' => false,
            'continue_if_empty' => false,
        ),
        1 => array(
            'name' => 'quantity',
            'required' => true,
            'filters' => array(),
            'validators' => array(),
            'description' => 'quantity of product',
            'error_message' => 'You must include a quantity',
            'allow_empty' => false,
            'continue_if_empty' => false,
        ),
        2 => array(
            'name' => 'tax',
            'required' => true,
            'filters' => array(),
            'validators' => array(),
            'description' => 'Add the VAT, GST, Sales Tax that will be applicable to this item. Use 0.00 for no value.',
            'error_message' => 'Please add a tax value, 0.00 for no value.',
            'allow_empty' => false,
            'continue_if_empty' => false,
        ),
        3 => array(
            'name' => 'quantity_accumulation',
            'required' => true,
            'filters' => array(
                0 => array(
                    'name' => 'Zend\\Filter\\Boolean',
                    'options' => array(),
                ),
            ),
            'validators' => array(),
            'description' => 'Either accumulate the entered quantity to the current basket quantity or set as the entered quantity.',
            'allow_empty' => false,
            'continue_if_empty' => false,
            'error_message' => 'Quantity accumulation field error.',
        ),
    ),

调用PUT方法时:

https://cloud.mysite.dev:8890/api/updatecart/1?prod_id=1&quantity=1&update_type=1&tax=0.00

我一直收到失败的验证错误:

{
"validation_messages": {
"prod_id": [
"Missing product id"
],
"quantity": [
"You must include a quantity"
],
"tax": [
"Please add a tax value, 0.00 for no value."
],
"quantity_accumulation": [
"Quantity accumulation field error."
]
},
"type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
"title": "Unprocessable Entity",
"status": 422,
"detail": "Failed Validation"
}

1 个答案:

答案 0 :(得分:1)

您需要以json格式提供数据作为http正文(有效负载)。 在没有查询参数的情况下调用URI。

https://cloud.mysite.dev:8890/api/updatecart/1

{
    "prod_id": 1,
    "quantity": 1,
    "update_type": 1,
    "tax": "0.00"
}

还要确保提供正确的请求标题:

Accept: application/json
Content-Type: application/json