【发布时间】:2019-01-30 11:37:55
【问题描述】:
我正在尝试将 JSON 格式的数据发布到 API:
我的JSON 数据是:
{
"itemData": {
"Name": "test",
"Priority": "High",
"SpecificContent": {},
"DeferDate": "2019-01-22T11:21:09.431Z",
"DueDate": "2019-01-22T11:21:09.432Z",
"Reference": "toto"
}
}
我的代码:
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", mytoken);
var res = client.PostAsync(MyURL,
new StringContent(JsonConvert.SerializeObject(new { ItemData = new { Name = "toto", Priority = "High", SpecificContent = new { } } }),
Encoding.UTF8, "application/json"));
try
{
res.Result.EnsureSuccessStatusCode();
MessageBox.Show(res.Result.EnsureSuccessStatusCode().ToString());
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
我有一个错误:
statusCode: 400 错误请求
我的代码有什么问题?
感谢您的帮助
【问题讨论】:
标签: c# json post dotnet-httpclient