【问题标题】:Error while trying to Create New Product Listing in Shopify using HTTPWebRequest in C#尝试使用 C# 中的 HTTPWebRequest 在 Shopify 中创建新产品列表时出错
【发布时间】:2021-01-28 08:50:01
【问题描述】:

下面是我的代码。从 shopify 获取令牌工作正常。但是,在创建新产品时,它一直给我一个错误。我已经尝试了所有可能的方法,但仍然无法正常工作。任何建议将不胜感激。

以下是我调用 CreateNewProduct 方法的方法,该方法传递来自 shopify 的访问令牌和带有产品端点的商店名称。

CreateNewProduct(accessTokenDTO.access_token, "https://{myshopname}.myshopify.com/admin/api/2020-10/products.json");

方法如下。

   public static void CreateNewProduct(string token, string Url)
    {
        Uri shopUri = new Uri(Url);
        HttpWebRequest GETRequest = (HttpWebRequest)WebRequest.Create(shopUri);
        GETRequest.ContentType = "application/json";
        GETRequest.Headers.Add("X-Shopify-Access-Token", token);
        GETRequest.PreAuthenticate = true;
        GETRequest.Method = "PUT";

        using (var streamWriter = new StreamWriter(GETRequest.GetRequestStream()))
        {
            string json = "{\"product\": { \"title\": \"Burton Custom Freestyle 151\", \"body_html\": \"<strong>Good snowboard!</strong>\",     \"vendor\": \"Burton\", \"product_type\": \"Snowboard\", \"tags\": [ \"Barnes & Noble\", \"John's Fav\", \"\\Big Air\\]}";
            streamWriter.Write(json);
            streamWriter.Flush();
        }

        HttpWebResponse GETResponse = (HttpWebResponse)GETRequest.GetResponse();

        var encoding = ASCIIEncoding.ASCII;
        using (var reader = new System.IO.StreamReader(GETResponse.GetResponseStream(), encoding))
        {
            string responseText = reader.ReadToEnd();
            Debug.WriteLine("Response Text: " + responseText);
        }

        GETResponse.Close();


    }

【问题讨论】:

  • 出现错误,不起作用......这些语句并没有太大帮助。你能在你的帖子中给出错误信息吗
  • 异常抛出:System.dll 中的“System.Net.WebException” System.dll 中发生“System.Net.WebException”类型的未处理异常远程服务器返回错误:(400)错误要求。 @贾瓦德

标签: c# html json shopify httpwebrequest


【解决方案1】:

400 BadRequest 通常是指您发送的请求正文根据 api 无效。

当我查看应该是 json 的字符串时,它在末尾显示无效数据。

[ \"Barnes & Noble\", \"John's Fav\", \"\\Big Air\\]}";

Big Air 之后您缺少结束引号。此外,不确定那些反斜杠是否应该出现在 Big Air 周围,但肯定是缺少的结束引号似乎是问题

【讨论】:

  • 我已经更改了 json 并删除了 ,\"\\Big Air\\ 但仍然收到相同的 400 错误请求。
  • 非常感谢您的帮助。这是导致问题的 Json 格式。
【解决方案2】:

json 格式不正确并且方法是 PUT 而不是 POST 存在问题。请参阅下面的工作代码。

    public static void CreateNewProduct(string token, string Url)
    {
        Uri shopUri = new Uri(Url);
        HttpWebRequest GETRequest = (HttpWebRequest)WebRequest.Create(shopUri);
        GETRequest.ContentType = "application/json";
        GETRequest.Headers.Add("X-Shopify-Access-Token", token);
        GETRequest.PreAuthenticate = true;
        GETRequest.Method = "POST";

        using (var streamWriter = new StreamWriter(GETRequest.GetRequestStream()))
        {
            string json = "{\"product\": { \"title\": \"Burton Custom Freestyle 151\", \"body_html\": \"<strong>Good snowboard!</strong>\",     \"vendor\": \"Burton\", \"product_type\": \"Snowboard\"} }";
            streamWriter.Write(json);
            streamWriter.Flush();
        }

        HttpWebResponse GETResponse = (HttpWebResponse)GETRequest.GetResponse();

        var encoding = ASCIIEncoding.ASCII;
        using (var reader = new System.IO.StreamReader(GETResponse.GetResponseStream(), encoding))
        {
            string responseText = reader.ReadToEnd();
            Debug.WriteLine("Response Text: " + responseText);
        }

        GETResponse.Close();
    }

【讨论】:

    猜你喜欢
    • 2015-04-04
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    相关资源
    最近更新 更多