次の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]);
}
}
ただし、これを実行するたびに、エラーの詳細がない400Badリクエストが表示されます。在庫は問題なく読めます。