简体   繁体   English

Shopify API更新清单结果导致“必填参数丢失或无效”

[英]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: 按照官方的Shopify API示例,我创建了以下请求以更新广告资源:

[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 在论坛上,我找到了这个答案,但没有帮助: https : //ecommerce.shopify.com/c/shopify-apis-and-technology/t/what-does-errors-variant-required-parameter缺少或无效的平均值270461

What am I doing wrong? 我究竟做错了什么?

Thanks. 谢谢。

EDIT: here's the final request CURL opts and raw response (<...> - withheld): 编辑:这是CURL选择的最终请求和原始响应(<...>-保留):

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

Here is a PHP example with cURL: 这是带有cURL的PHP​​示例:

<?php
$ch = curl_init("https://key:pass@yourstore.myshopify.com/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? 如果仍然遇到问题,您是否愿意共享一些代码?

The problem is curl overrides Content-Type header if using CURLOPT_PUT or CURLOPT_POST. 问题是,如果使用CURLOPT_PUT或CURLOPT_POST,则curl会覆盖Content-Type标头。 Needed to remove these and use CURLOPT_CUSTOMREQUEST. 需要删除这些并使用CURLOPT_CUSTOMREQUEST。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM