Request Works in Postman, but the Generated RestSharp Code Fails

Following request works in the Postman UI (see image below). But the code generated for c# (RestSharp) fails with StatusCode: Unauthorized.

The request works in Postman before and after the code fails.

Generated code (which fails):
var client = new RestClient(“$filter=[… request filter here …]”);
var request = new RestRequest(Method.GET);
request.AddHeader(“cache-control”, “no-cache”);
request.AddHeader(“Connection”, “keep-alive”);
request.AddHeader(“Accept-Encoding”, “gzip, deflate”);
request.AddHeader(“Cookie”, “visid_incap_1968058=sFqg5[… rest of cookie …]”);
request.AddHeader(“Host”, “”);
request.AddHeader(“Postman-Token”, “7eb7d7a[… rest of postman token …]”);
request.AddHeader(“Cache-Control”, “no-cache”);
request.AddHeader(“Accept”, “/”);
request.AddHeader(“User-Agent”, “PostmanRuntime/7.15.2”);
request.AddHeader(“Authorization”, “Bearer eyJ0eXAiO[… rest of access token …]”);
IRestResponse response = client.Execute(request);

Based on searches so far, have tried the following:

  • replace 'Bearer ’ with 'bearer ’
  • replace 'Bearer ’ with ‘JWT’ or ‘jwt’
  • remove the ‘Accept’ header

Works in Postman (note the data returned in the Body):

Any suggestions appreciated.

I don’t think what you have searched and tried so far will have any success.

You need to understand what kind of an authorization you are dealing with so that you get fetch new tokens when needed.

It is possible that the access token expired. Usually, the response body contains more clues on why the authentication failed.

I am having a similar problem Did you ever solve it?

I am having same issue, with VS 15 and Postsharp - v4.0.30319, any help appreciated

the error i am getting is - { “error”: “The requested resource is not available” }

in C#, try with this code:

        RestClient client = new RestClient("https://....");
        RestRequest request = new RestRequest( Method.GET); // or Method.POST
        request.AddHeader("Authorization", "Bearer " + "your token here");
        request.AddHeader("Cache-Control", "no-cache");
        request.RequestFormat = DataFormat.Json;

        JavaScriptSerializer js = new JavaScriptSerializer();
        dynamic obj = js.Deserialize<dynamic>("your Json body string");


        IRestResponse response = client.Execute(request);

Good Luck!