c# - 使用 HttpClient 发布 JSON 数据时出现错误 400
问题描述
我正在尝试以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 错误请求
我的代码有什么问题?
感谢您的帮助
解决方案
早晨!
400 错误请求 -
这只是意味着您发送的请求有一些不合适的地方。
我的第一个检查通常是确保您发送的对象与 API 端点所期望的数据完全相同:也许您可以向我们提供您正在调用的 API 的一些详细信息?
推荐阅读
- arm - STM32上的数据、指令和flash CCM RAM有什么区别
- lotusscript - 从嵌入视图打开的对话框显示对话框形式,而不是直接返回嵌入视图
- angular - 如何在 Angular 中预览 Docx 文件
- android - 如何从 ImageView 获取像素值
- cmd - 用 4 个维度和多个变量抓取到 netcdf
- graphviz - graphviz - 如何在直箭头上创建标签
- python - 使用 elemettree 获取 XML 中特定标签的内容
- html - 按钮的浏览器兼容性
- powershell - 复制用户和组
- jenkins - jenkins Multibranch 管道计划与 bitbucket 合并事件一起运行