What is the proper way to add a named parameter to a RestRequest?

15 Views Asked by At

I just upgraded my RestSharp client from 106.15.0 to 110.2.0. I have been modifying my code to accommodate the breaking changes.

I am now creating and posting my request as follows:

var payload = JsonConvert.SerializeObject(tokenRequest, JsonSerializerSettings);
    
var request = new RestRequest(route);
    
request.AddHeader("ContentType", "application/json");
request.AddStringBody(payload, DataFormat.Json);

var response = restClient.Post(request);

This works correctly, but if I examine the request.Parameters, the payload is in an unnamed parameter.

However, if I change request.AddStringBody(payload, DataFormat.Json) to request.AddParameter(nameof(payload), payload), I can use the request.Parameters.TryFind("payload") method to retrieve the payload, but then when I call restClient.Post(request) it throws a "BadRequest" exception.

What am I doing wrong?

0

There are 0 best solutions below