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(ā€œhttps://api.xyz.com/odata/Property?$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ā€, ā€œapi.xyz.comā€);
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");

        request.AddJsonBody(obj);

        IRestResponse response = client.Execute(request);

Good Luck!
Cristian.

This is still an issue i.e. generated C# code in Postman does not compile with latest RestSharp: