這是我的代碼:
request.AddHeader("Content-Type", "application/json; charset=utf-8");
request.RequestFormat = DataFormat.Json;
request.AddJsonBody(new
{
AccountName= "Some account name",
AccountStatus= true
});
request.AddParameter("Content-Type", "application/json; charset=utf-8", ParameterType.RequestBody);
request.RequestFormat = DataFormat.Json;
response = client.ExecutePostAsync(request).Result;
Console.WriteLine(response.Content.ToString());
}
我得到的回應是:400 (Bad Request)
然后從服務器日志中,我知道上面的代碼不維護 json Keys 的駱駝案例。它發送 asaccountname而不是AcconutName. 對AccountStatus.
uj5u.com熱心網友回復:
RestSharp在 Web 背景關系中使用 JSON 序列化的默認選項。您可以為客戶端更改這些:
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web);
options.PropertyNamingPolicy = null;
client.UseSystemTextJson(options);
上面的示例仍然使用 web 的默認選項,但洗掉了負責將屬性名稱序列化為駝峰式的 PropertyNamingPolicy。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/477185.html
