1

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

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

我的模块配置:

更新车设置:

 '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"
}
4

1 回答 1

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
于 2015-01-15T14:17:50.787 回答