Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AddUrlSegment throws NullReferenceException in RestSharp

Tags:

c#

.net

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.

like image 530
Oakcool Avatar asked Jul 11 '26 20:07

Oakcool


2 Answers

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

like image 120
Oakcool Avatar answered Jul 13 '26 10:07

Oakcool


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);
like image 24
Juri Avatar answered Jul 13 '26 10:07

Juri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!