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.