# Request Works in Postman, but the Generated RestSharp Code Fails

**URL:** <https://community.postman.com/t/request-works-in-postman-but-the-generated-restsharp-code-fails/7251>\
**Category:** Help Hub\
**Tags:** authentication, code\
**Created:** [August 19, 2019, 9:47pm UTC](https://community.postman.com/t/request-works-in-postman-but-the-generated-restsharp-code-fails/7251 "2019-08-19T21:47:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pevans360](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@pevans360](https://community.postman.com/u/pevans360)\
**Post date:** [August 19, 2019, 9:47pm UTC](https://community.postman.com/t/request-works-in-postman-but-the-generated-restsharp-code-fails/7251/1 "2019-08-19T21:47:18Z")

</div>

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=](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](http://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):

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/0/1/012c926a2d680982a3b354eba164a083ba33e719.png)

Any suggestions appreciated.

---

<div class="post-metadata">

**Author:** ![vdespa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vdespa/32/2770_2.png) [@vdespa](https://community.postman.com/u/vdespa)\
**Post date:** [August 20, 2019, 7:03am UTC](https://community.postman.com/t/request-works-in-postman-but-the-generated-restsharp-code-fails/7251/2 "2019-08-20T07:03:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![steve.schmalfeld](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@steve.schmalfeld](https://community.postman.com/u/steve.schmalfeld)\
**Post date:** [November 20, 2019, 7:50pm UTC](https://community.postman.com/t/request-works-in-postman-but-the-generated-restsharp-code-fails/7251/3 "2019-11-20T19:50:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tamojitmondal](https://avatars.discourse-cdn.com/v4/letter/t/ee59a6/32.png) [@tamojitmondal](https://community.postman.com/u/tamojitmondal)\
**Post date:** [February 4, 2020, 12:41pm UTC](https://community.postman.com/t/request-works-in-postman-but-the-generated-restsharp-code-fails/7251/4 "2020-02-04T12:41:40Z")

</div>

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” }

---

<div class="post-metadata">

**Author:** ![cristiangavilan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/cristiangavilan/32/5307_2.png) [@cristiangavilan](https://community.postman.com/u/cristiangavilan)\
**Post date:** [August 18, 2020, 2:32pm UTC](https://community.postman.com/t/request-works-in-postman-but-the-generated-restsharp-code-fails/7251/5 "2020-08-18T14:32:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![trisyswhatsapp](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/trisyswhatsapp/32/1371_2.png) [@trisyswhatsapp](https://community.postman.com/u/trisyswhatsapp)\
**Post date:** [November 24, 2022, 12:59pm UTC](https://community.postman.com/t/request-works-in-postman-but-the-generated-restsharp-code-fails/7251/6 "2022-11-24T12:59:31Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/e/0/e03d14058f665f220b2b8b334c312a4700da1d63.png)
