Oakcool
Oakcool

Reputation: 1496

AddUrlSegment throws NullReferenceException in RestSharp

I am trying to use RestSharp's AddUrlSegment to sub the token in a URL

ex: "www.test.com/{someToken}/Testing"

I am using this code:

string theToken = "someStringToken";
restRequest.AddUrlSegment("someToken",theToken);

This throws a NullReferenceException, when I try to execute the request.

Any ideas what I am doing wrong.

Thanks.

Upvotes: 3

Views: 3585

Answers (2)

Juri
Juri

Reputation: 3499

I got the same problem.

Here is some alternative solution code with passing resource in constructor and simplified method call, using RestSharp v.104.4.0:

string _baseUrl = "www.test.com";

var client = new RestClient(_baseUrl);
var req = new RestRequest("/{someToken}/Testing", Method.GET);
req.RequestFormat = DataFormat.Json;
req.AddUrlSegment("someToken", theToken);

Upvotes: 1

Oakcool
Oakcool

Reputation: 1496

Alright I figured this out. The version of RestSharp I have (NUGET), apparently does not support the method above. Also the Resource property is the one that should be getting the url that is going to be replaced so the final code is something like this.

string _baseUrl = "www.test.com";

RestClient client = new RestClient(_baseUrl);
RestRequest restRequest = new Request();
restRequest.Resource = "/{someToken}/Testing";
restRequest.AddParameter("someToken", theToken , ParameterType.UrlSegment);

This piece of code works with the version I got from NUGET

Upvotes: 2

Related Questions