1

I am new to ApiGility and am attempting to update my shopping cart via an API call as apposed to a route. I am using Zend Framework 2 with a code connected api.

The problem I am currently facing is that no matter what I try, I am unable to put the information to the api without validation errors.

My module.config:

Updatecart settings:

 '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',
    ),

Relevant filter settings:

'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.',
        ),
    ),

When calling the PUT method:

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

I keep getting Failed Validation errors:

{
"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"
}
HappyCoder
  • 5,985
  • 6
  • 42
  • 73

1 Answers1

1

You need to supply your data as http body (payload) in json format. Make the call to the URI without the query params.

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

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

Also make sure you supply the right request headers:

Accept: application/json
Content-Type: application/json
Bram Gerritsen
  • 7,178
  • 4
  • 35
  • 45
  • ok thanks, any idea how to do this via Chrome Postman? I have tried a few different methods but not quite sure how to get it right. tx – HappyCoder Jan 16 '15 at 08:10
  • Sure, I use it too. Put in your request URI, select `PUT` in the dropdown. Add the headers by clicking the headers button. You can insert the payload by selecting the button `raw`. – Bram Gerritsen Jan 16 '15 at 10:37