Unirgy
Unirgy

Reputation: 1535

Shopify API update inventory results in "Required parameter missing or invalid"

following the official Shopify API example, I've created the following request to update inventory:

[method] => PUT

[url] => https://<withheld>:<withheld>@test-shop-422.myshopify.com/admin/variants/1234567890.json

[headers] => Array (
     [0] => Accept: application/json
     [1] => Content-Type: application/json
     [2] => X-HTTP-Method-Override: PUT
)

[params] => {"variant":{"id":1234567890,"inventory_quantity":2,"old_inventory_quantity":1}}

[response] => {"errors":{"variant":"Required parameter missing or invalid"}}

On the forums I've found this answer, but it didn't help: https://ecommerce.shopify.com/c/shopify-apis-and-technology/t/what-does-errors-variant-required-parameter-missing-or-invalid-mean-270461

What am I doing wrong?

Thanks.

EDIT: here's the final request CURL opts and raw response (<...> - withheld):

Array
(
    [CURLOPT_AUTOREFERER] => 1
    [CURLOPT_CAINFO] => <...>/cacert.pem
    [CURLOPT_CONNECTTIMEOUT] => 5
    [CURLOPT_FOLLOWLOCATION] => 1
    [CURLOPT_HEADER] => 1
    [CURLOPT_HTTPHEADER] => Array
        (
            [0] => Accept: application/json
            [1] => Content-Type: application/json
            [2] => X-HTTP-Method-Override: PUT
            [3] => Expect: 
            [4] => Referer: <...>/process_queue
            [5] => Content-Length: 47
        )

    [CURLOPT_MAXREDIRS] => 10
    [CURLOPT_POSTFIELDS] => {"variant":{"inventory_quantity_adjustment":1}}
    [CURLOPT_PUT] => 1
    [CURLOPT_RETURNTRANSFER] => 1
    [CURLOPT_SSL_VERIFYHOST] => 2
    [CURLOPT_SSL_VERIFYPEER] => 1
    [CURLOPT_TIMEOUT] => 5
    [CURLOPT_URL] => https://<...>:<...>@test-shop-422.myshopify.com/admin/variants/26020635016.json
    [CURLOPT_USERAGENT] => Mozilla/5.0
)

Raw response:

HTTP/1.1 400 Bad Request
Server: nginx
Date: Wed, 12 Oct 2016 01:23:05 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Frame-Options: DENY
X-ShopId: 9690230
X-ShardId: 7
X-Shopify-Shop-Api-Call-Limit: 1/40
HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT: 1/40
X-Stats-UserId: 0
X-Stats-ApiClientId: 1444946
X-Stats-ApiPermissionId: 31937062
X-XSS-Protection: 1; mode=block; report=/xss-report/<...>?source%5Baction%5D=update&source%5Bcontroller%5D=admin%2Fproduct_variants&source%5Bsection%5D=admin
X-Request-Id: <...>
X-Dc: chi2
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none
X-Content-Type-Options: nosniff

{"errors":{"variant":"Required parameter missing or invalid"}}

Upvotes: 0

Views: 1664

Answers (2)

Unirgy
Unirgy

Reputation: 1535

The problem is curl overrides Content-Type header if using CURLOPT_PUT or CURLOPT_POST. Needed to remove these and use CURLOPT_CUSTOMREQUEST.

Upvotes: 0

Josh Brown
Josh Brown

Reputation: 4096

Here is a PHP example with cURL:

<?php
$ch = curl_init("https://key:[email protected]/admin/variants/25930937097.json");
$variant = array('variant' => 
    array(
        'inventory_quantity' =>  2, 
        'old_inventory_quantity' => 1
    )
);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($variant)); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$response = curl_exec($ch);
print_r($response);

If you're still having trouble, would you mind sharing some of your code?

Upvotes: 2

Related Questions