我正在尝试使用以下 json 和 c# 代码更新 shopify 中现有变体的库存:
var p = new
{
id = "220994352",
inventory_quantity = "0"
};
string url = "https://somestore.myshopify.com/admin/variants/220994352.json";
ShopifyAuthorizationState authState = (ShopifyAuthorizationState)HttpContext.Current.Session["shopify_auth_state"];
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "PUT";
request.ContentType = "application/json";
request.Headers.Add("X-Shopify-Access-Token", authState.AccessToken);
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(p);
json = "{ variant: " + json + "}";
if (!String.IsNullOrEmpty(json))
{
using (var ms = new MemoryStream())
{
using (var writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(json);
writer.Close();
}
}
}
string result = "";
try
{
var response = (HttpWebResponse)request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
StreamReader sr = new StreamReader(stream);
result = sr.ReadToEnd();
sr.Close();
}
}
catch (WebException ex)
{
WebHeaderCollection webHeaderCollection = ex.Response.Headers;
foreach (var header in ex.Response.Headers.AllKeys)
{
Console.WriteLine(webHeaderCollection[header]);
}
}
但是,每次我运行它时,我都会收到一个 400 Bad 请求,但没有关于错误的详细信息。我可以毫无问题地阅读库存。