尝试在 C# 中使用 HTTPWebRequest 在 Shopify 中创建新产品列表时出错

Error while trying to Create New Product Listing in Shopify using HTTPWebRequest in C#

下面是我的代码。从 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();


    }

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

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

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

您在 Big Air 之后缺少收盘价。另外,不确定那些反斜杠是否应该出现在 Big Air 周围,但绝对缺少结束引号似乎是问题所在

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();
    }