Issue while creating product using Shopify API

Viewed 401

I am experiencing a weird issue while exporting some products to Shopify. When I save a JSON generated using JSON.net to txt file and then read it and then send it to Shopify's create product API, it works fine. However, if I just save the JSON to string and send post request directly, I get a 502 gateway error from Shopify. This happens only when creating certain products, where the JSON payload is somewhat bigger than normal. On other products, just saving JSON to a string and then sending to Shopify works OK. Weirdly, when I save the JSON to file from C#, then read that file a few lines below in the same code, and use that content as JSON string and send a post request, it again doesn't work. What do you think could be the issue?

Here's the JSON generated using JSON.net which works OK when read from file (https://pastebin.com/9SKFPjGJ)

var jsonContent = JsonConvert.SerializeObject(myObject);
using (var requestMessage = new HttpRequestMessage(HttpMethod.Put, $"https://{shopifySlug}.myshopify.com/admin/products/{originalShopifyProductId}.json"))
{
    requestMessage.AddShopifyHeaders(shopifyAccessToken); // just a method for adding necessary shopify headers
    requestMessage.Content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
    response = await client.SendAsync(requestMessage);
}

Its getting hard for me to know the reason.

Here's what works:

Code that works:
json.txt was generated using this separately:

System.IO.File.WriteAllText("E:\\json.txt",JsonConvert.SerializeObject(myObject));

(Note that if the above line of code is inserted before the code below in the same file, then the code below doesn't work, i.e. the file has to be saved separately for it to work).

And then, just reading the JSON from file and sending that in the POST request works. Code below:

var jsonContent = System.IO.File.ReadAllText("E:\\json.txt");
using (var requestMessage = new HttpRequestMessage(HttpMethod.Put, $"https://{shopifySlug}.myshopify.com/admin/products/{originalShopifyProductId}.json"))
{
    requestMessage.AddShopifyHeaders(shopifyAccessToken); // just a method for adding necessary shopify headers
    requestMessage.Content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
    response = await client.SendAsync(requestMessage);
}

Also the not working code at the very top works OK for smaller JSON payloads like this one https://pastebin.com/RhYjVuvv

0 Answers
Related