-1

I need to call client API-HTTPS (GET) with some specific headers

Accept:application/json,application/vnd.error+json
Date:2018-10-03T06:52:48Z
Authorization:<SECRETKEY>

In this scenario when I try to call it from client like Postman or Advanced REST client, API give proper result. But the same thing I tried with C# code which generates 401-Unauthorized errorcode.

For example If I'm using postman code for RestSharp (C#). It won't work from my code. Postman screenshot:
Postman screenshot

Even I tried the same with HttpWebRequest and HttpClient too. But no luck.

Code which postman provided from code section is as below with some confidential information which I can't expose.

var client = new RestClient("https://**CLIENT_API_PATH**");
var request = new RestRequest(Method.GET);
request.AddHeader("Postman-Token", "ef8c2a79-a501-4cef-aa2e-bacdc9d3a922");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Authorization", "**SECRET_KEY**");
request.AddHeader("Date", "2018-10-03T06:52:48Z");
request.AddHeader("Accept", "application/json,application/vnd.error+json");
IRestResponse response = client.Execute(request);

In above code 3 parameters are compulsory and have to pass for successful call to API.

  1. Date with the above mentioned format only.

  2. Accept with above mentioned string only.

  3. Authorization with specific authentication (custom by client).

As per suggested comment I also add fiddler data from the call.

  1. Postman request fetch from fiddler fiddler screenshot
  2. C# api call code request from fiddler fiddler screenshot
  3. C# code debug result visual studio debug screenshot
  • 1
    I didn't understand, does it work with the RestSharp code ? Where is your C# code? – sagi Oct 03 '18 at 12:10
  • 3
    please provide your code – er-sho Oct 03 '18 at 12:11
  • 1
    Code is in https://i.stack.imgur.com/rb59o.jpg. Some client specific detail which I can't expose. – manishjain-kcspl Oct 03 '18 at 12:14
  • 1
    i mean backend c# code. – er-sho Oct 03 '18 at 12:15
  • 1
    No @sagi my client API does not calling from Restsharp code provided by postman. – manishjain-kcspl Oct 03 '18 at 12:17
  • 1
    We can not provide assistance for an issue of which you are not showing us the code used, you are instead giving us code which does work to tell us that it works? Is the RestSharp code above what's in your production application? – ColinM Oct 03 '18 at 12:39
  • Can you check what request is being sent by your C# code by using a look like `Fiddler`? Probably you are not sending `Authorization` header or not in the format excepted by the API. – TheVillageIdiot Oct 03 '18 at 12:53
  • @ColinM : Yes above mentioned code is my production code using which I'm trying to call client API. This code is same from **code** section from postman by which API call was successful. – manishjain-kcspl Oct 04 '18 at 04:02
  • In that case, you need to provide the Authorization type. `request.Headers.Add("Authorization", "Bearer **SECRET_KEY**");` This is what the answer below contains, but doesn't actually call out. – ColinM Oct 04 '18 at 09:18
  • @ColinM : What suggested by '@muhammad-faisal' is by 'HttpWebRequest' but that doesn't work as I tried with 3 different libraries. – manishjain-kcspl Oct 05 '18 at 05:32
  • He suggests `HttpWebRequest`, and part of that code uses the correct format for Authorization tokens. – ColinM Oct 05 '18 at 10:56
  • @ColinM: Yes you are right, but I trying to say that I tried with all that effort with different libraries and different way. But Now I only required which way I can achieve this. – manishjain-kcspl Oct 08 '18 at 03:43

1 Answers1

0

Try this.

public static string Get(Uri uri, string token)
{
    string responseString = string.Empty;
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);

    request.Method = "GET";
    request.ContentType = "application/json;charset=utf-8";
    request.Headers.Add("Authorization", string.Format("Bearer {0}", token));

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        using (Stream responseStream = response.GetResponseStream())
        {
            StreamReader responseReader = new StreamReader(responseStream);
            responseString = responseReader.ReadToEnd();
        }
    }
    return responseString;
}
  • 2
    "Try this" is not an answer. Explain what your code does and why that would solve the OP's problem. – CodeCaster Oct 03 '18 at 12:22
  • 2
    Please confirm one thing. Which Authorization mode you are using while initiating request from Postman? Is it Bearer or something else? – Muhammad Faisal Oct 03 '18 at 12:27
  • 2
    @muhammad-faisal. I have to pass 2 more headers with **Authorozation**. Which are **Date** and **Accept**. But when I tried to put Date with above mentioned string it throws "Argument exception". Other alternative I already tried. – manishjain-kcspl Oct 03 '18 at 12:28