Shopify api 更新变体返回错误

Shopify api updating variants returned error

我想更新商店产品的 inventory_quantity,但出现错误

The remote server returned an error: (400) Bad Request.

我就是这样实现的。

ProductVariant product = new ProductVariant();
product.id = 1962693211;
product.inventory_quantity = 20;
product.inventory_management = "shopify";

string ordersApi = ConfigurationManager.AppSettings["Shopify_Api_Inventory"];
string url = ordersApi.Replace("{StoreName}", _cred.StoreName);
url = url.Replace("{id}", product.id.ToString());
string xmlStringResult = string.Empty;


try
{
    var req = (HttpWebRequest)WebRequest.Create(url);
    req.Method = "PUT";
    req.ContentType = "application/json";
    req.Credentials = GetCredential(url);
    req.PreAuthenticate = true;

    var json = JsonConvert.SerializeObject(product);
    json = "{ variant: " + json + "}";



    if (!String.IsNullOrEmpty(json))
    {
        using (var ms = new MemoryStream())
        {
            using (var writer = new StreamWriter(req.GetRequestStream()))
            {
                writer.Write(json);
                writer.Close();
            }
        }
    }

    using (var resp = (HttpWebResponse)req.GetResponse())
    {
        if (resp.StatusCode != HttpStatusCode.OK)
        {
            string message = String.Format("Call failed. Received HTTP {0}", resp.StatusCode);
            AppendLog(string.Format("Error: {0}", message));
            return xmlStringResult;
        }

        var sr = new StreamReader(resp.GetResponseStream());
        xmlStringResult = sr.ReadToEnd();
    }
}
catch (Exception ex)
{
    AppendLog(string.Format("Error: {0}", ex.Message));
}

提前感谢您的帮助。

这行代码

json = "{ variant: " + json + "}";

产生以下 json

{ variant: {"id":1962693211,"inventory_quantity":20,"inventory_management":"shopify"}}

这是无效的。有效的 json 应该是

{ "variant": {"id":1962693211,"inventory_quantity":20,"inventory_management":"shopify"}}

所以需要把上面这行代码改成这样

json = "{ \"variant\": " + json + "}";