我正在尝试使用以下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请求,但没有关于错误的详细信息。 我没有任何问题就阅读了库存。
答案 0 :(得分:1)
您是否确定在授权应用程序时设置了作用域权限,以便对产品具有写入权限?
您是否可以发布原始http响应的链接,以显示确切返回的内容,包括标题以查看是否还有其他详细信息?
答案 1 :(得分:1)
400错误请求通常意味着发送的数据无效。看着你的json可能对你有所帮助。尝试在json字符串中将变量放在变量周围。