'RestSharp: Adding content-type

In a previous version of RestSharp I was able to add a content-type:application/json

        RestClientOptions options = new RestClientOptions();
        options.BaseUrl = new Uri($"https://{_options.Auth0Domain}");
        var client = new RestClient(options);

        var request = new RestRequest("/oauth/token") { Method = Method.Post };
        request.AddHeader("content-type", "application/json");
        request.AddParameter("application/json", json, ParameterType.RequestBody);
        var response = await client.ExecuteAsync<Auth0MachineToMachineResponse>(request);

But after the big 107 release I get this error when I try to add the content-type, which is required by the end point I am calling:

"Misused header name, 'content-type'. Make sure request headers are used with HttpRequestMessage



Solution 1:[1]

Please do not add content type manually.

Use AddStringBody and the content type for the body parameter instead.

var request = new RestRequest("/oauth/token").AddStringBody(json, "application/json");
var response = await client.PostAsync<Auth0MachineToMachineResponse>(request);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alexey Zimarev