ff8mania
ff8mania

Reputation: 1770

RestCall with Restsharp not authenticated, it is with Postman

I'm trying to get info from a REST endpoint that requires basic authentication. Using postman I'm fine, getting the information I need from the call:

GET endpoint/api/workitems?ids=20449& api-version=2.0 HTTP/1.1
Host: xxx.xxx.xxx.50:8080
Authorization: Basic ABC==,Basic ZZZ    cache-control: no-cache
Postman-Token: e6476d89-ec2b-439d-8821-88ef446a03a9

When I do the same with restsharp, I get an Unauthorized error:

var client = new RestClient("http://xxx.xxx.xxx.50:8080/endpoint/api/workitems?ids=20449& api-version=2.0");
var request = new RestRequest(Method.GET);
request.AddHeader("Postman-Token", "06ea7553-d35e-4743-a516-201d6e3b9084");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Authorization", "Basic ABC==,Basic ZZZ");
IRestResponse response = client.Execute(request);

Am I missing something?

Thanks

Upvotes: 0

Views: 1198

Answers (1)

ff8mania
ff8mania

Reputation: 1770

Eventually I found that the proper way to do the Basic Authentication with restsharp is the following.

It works:

var client = new RestClient("http://xxx.xxx.xxx.50:8080/endpoint/api/workitems?ids=20449& api-version=2.0");
client.Authenticator = new HttpBasicAuthenticator(username, decodedToken);
var request = new RestRequest(Method.GET);
request.AddHeader("Postman-Token", "06ea7553-d35e-4743-a516-201d6e3b9084");
request.AddHeader("cache-control", "no-cache");
IRestResponse response = client.Execute(request);

Upvotes: 1

Related Questions