RestSharp:添加 content-type

RestSharp: Adding content-type

在以前版本的 RestSharp 中,我能够添加 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);

但是在 big 107 发布之后,当我尝试添加 content-type 时出现此错误,这是我调用的端点所必需的:

“误用了 header 名称,'content-type'。确保请求 header 与 HttpRequestMessage

一起使用

试试:

request.AddParameter("Content-Type", "application/x-www-form-urlencoded", ParameterType.HttpHeader);

现在好像区分大小写了?

请不要手动添加内容类型。

改用 AddStringBody 和正文参数的内容类型。

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