-3

How could we call the below endpoints in C# webClient, I have managed this with GoogleScript but now I need it in C#

      var options = {
    "async": true,
    "crossDomain": true,
    "method" : "GET",
    "headers" : {
      "X-Api-Key" : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      "X-Api-Secret": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    }
  };

  var response = UrlFetchApp.fetch('https://dashboard.reviewpush.com/api/company/locations',options);
  var pages = JSON.parse(response.getContentText());

how we can pass headers with multiple values to the request, the code that i have tried is to as below

public static void Main (string[] args)
{
    string url = "https://dashboard.reviewpush.com/api/company/locations";

    using (var client = new WebClient())
    {
        client.Headers[HttpRequestHeader.ContentType] = "application/json";
        client.Headers["X-IFORM-API-REQUEST-ENCODING"] = "JSON";
        client.Headers["async"] ="true";
        client.Headers["crossDomain"] = "true";
        client.Headers["method"] = "GET";
        client.Headers["headers"] = "{'X-Api-Key' : 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
          'X-Api-Secret': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
}";

        Stream data = client.OpenRead (args[0]);
        StreamReader reader = new StreamReader (data);
        string s = reader.ReadToEnd ();
        Console.WriteLine (s);
        data.Close();
        reader.Close();
}
SML Analysis
  • 11
  • 1
  • 1
  • 3

1 Answers1

0

The problem is that you are mixing up options of the library with HTTP headers. Try with this instead:

public static void Main (string[] args)
{
    string url = "https://dashboard.reviewpush.com/api/company/locations";

    using (var client = new WebClient())
    {
        client.Headers[HttpRequestHeader.ContentType] = "application/json";
        client.Headers["X-Api-Key"] = "key";
        client.Headers["X-Api-Secret"] = "secret";

        string s = client.DownloadString(url);
        Console.WriteLine(s);
    }
}

You might want to look into the recommended HttpClient if you can use .NET 4.5 or newer.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120