2

I had a perfectly working code, written in C#, which used to fetch data from Shopify API on daily basis but for the past few days it has started giving me an error. I am having issues with tracing what exactly could be the source of this problem.

If I make API calls through Web Browser or RESTAPI Client, data retrieval is successful everytime.

Also, Since I was having no luck with this code, I did a code migration from C# to Python and when I ran my Python script, data retrieval was successful without any errors.

My C# code looks something like this:

    public override void CreateNewOutputRows()
{
    string tablename = "products";
    string filepath = Variables.DATAFILEPATH + "SHOPIFY_" + tablename.ToUpper() + ".csv";
    string apiKey = "xxxxxxxxxxxx";
    string password = "xxxxxxxxx";
    string json = GetSomeREST(apiKey, password, "admin/" + tablename);
    KSONConverter(json, filepath);

}

private string GetSomeREST(string apiKey, string password, string path, int offset = 0, string listId = "")
{
    var uri = string.Format("https://xxxxxxx.myshopify.com/admin/products.json");
    try
    {
        using (var webClient = new System.Net.WebClient())
        {
            //webClient.Headers.Add("Accept", "application/json");
            //webClient.Headers.Add("Authorization", "apikey " + apiKey);
            webClient.Headers.Add("X-Shopify-Access-Token", password);
            return webClient.DownloadString(uri);
        }
    }
    catch (System.Net.WebException we)
    {
        using (var sr = new System.IO.StreamReader(we.Response.GetResponseStream())
       //getting error on the above line of code

        {
            return sr.ReadToEnd();
        }
    }

}

public void KSONConverter(string json, string filepath)
{
    // System.IO.File.AppendAllText(filepath, json);
    RootObject ro = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json);
    string csv = "";
    foreach(Product p in ro.products)
    {
        csv = csv + p.id + "|";
        csv = csv + p.title.Replace('|',' ') + "|";
        //csv = csv + p.body_html + "|";
        csv = csv + p.vendor.Replace('|', ' ') + "|";
        csv = csv + p.product_type.Replace('|', ' ') + "|";
        csv = csv + p.created_at + "|";
        csv = csv + p.handle + "|";
        csv = csv + p.updated_at + "|";
        csv = csv + p.published_at + "|";
        csv = csv + p.template_suffix + "|";
        csv = csv + p.published_scope + "|";
        csv = csv + p.tags;
        csv = csv + "\n";
    }
    System.IO.File.WriteAllText(filepath, csv);

}

My expected output should be a CSV file, containing the data fetched from Shopify API but instead, I am getting the error "Object reference not set to an instance of an object" on the part of code mentioned above //getting error on the above line of code

OmarAmjad
  • 21
  • 1
  • From the [docs for `WebException.Response`](https://learn.microsoft.com/en-us/dotnet/api/system.net.webexception.response?view=netframework-4.8#System_Net_WebException_Response): *If a response is available from the Internet resource, a WebResponse instance that contains the error response from an Internet resource; **otherwise, null.*** Thus `Response` must be null here. See: [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/q/4660142/3744182). Examine the contents of `we` by printing `we.ToString()` to see what the real problem is. – dbc Sep 05 '19 at 07:11
  • I got it working. Turns out the TLS/SSL version was not being called by default whenever the Web Request was being made. Defining it explicitly and calling it then made the error go away. – OmarAmjad Sep 12 '19 at 07:16

0 Answers0