0

I have a situation where part of my code sets a URL segment to a value, and then calls the code that executes the request with a retry if authentication fails.

Part of refreshing the authentication data retrieves the correct value of the URL segment. So, I need to call AddUrlSegment again with the new, proper value and then execute the request again, now with the proper value.

But that doesn't work. RestSharp always uses the original value of the URL segment. Here is a mock up of the code that illustrates it:

 RestClient client = new RestClient();
 var request = new RestRequest("https://api.example.com/orgs/{segment}/something");
 request.AddUrlSegment("segment", 1);
 var url1 = client.BuildUri(request);
 request.AddUrlSegment("segment", 2);
 var url2 = client.BuildUri(request);

After running this code, I would have expected url1 to be {https://api.example.com/orgs/1/something}

which it is. And I would have expected url2 to be {https://api.example.com/orgs/2/something}

which it is not. It is the same as url1.

Is there something different that I need to do to get url2 to use the updated segment value?

2
  • I don't know this API, but I guess you need to remove the thing added by the first AddUrlSegment call (if that's even possible!) before you try to add another (i.e. either the second add is ignored, because it already has a value for the segment called "segment", or the first call takes precedence). Or alternatively, you need to create a new RestRequest. Commented Oct 17 at 14:49
  • Yea, I can't create a new request, this is the code that abstracts away the code for handling a token refresh, so the code knows nothing about the request being made. With some unit test debugging, I found out that the AddUrlSegment becomes a parameter on the request and, indeed two of them exist after the second call. There is a removeParamter call that I used to remove the first one before calling the second one. Commented Oct 18 at 20:51

1 Answer 1

1

It turns out that the AddUrlSegment just adds a Parameter onto the request with a type of UrlSegment, a name of the segment name and a value of the value you pass in.

When calling AddUrlSegment the second time, a second paramter is added to the request and clearly the url generation code just searches the request for the first instance of the Parameter and uses that value. I would think that a quick fix to this problem would be to search for the last instance of the Parameter rather than the first.

It is also clear from my debugging that when you call AddUrlSegment, it doesn't first look for an instance of the Parameter to modify, it just blindly adds a new one.

But, because it is a Parameter on the request, you can use the Parameter manipulation functions to find the existing instance of the parameter and then remove it before adding the new value.

This also gives me another idea, that since I just found the Parameter instance, I could try just changing it's value rather than remove the Parameter instance and then call AddUrlSegment a second time.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.