HttpClient.PostAsync 将 JSON 传递给 API 时导致 400 错误
HttpClient.PostAsync causes 400 error when passing JSON to API
我正在尝试将一些 JSON 格式的数据传递给 Bugzilla API 但我收到了 400
响应。我正在使用 Netwonsoft.Json
生成 JSON,据我所知,它生成的很好,所以我不确定是什么导致了 400 错误。
代码:
var Client = new HttpClient();
Dictionary <string, string> BugData = new Dictionary<string, string>
{
{ "Bugzilla_api_key", "Removed for scurity" },
{ "product", "Test" },
{ "component", "Test Component" },
{ "version", "unspecified" },
{ "summary", "Basic API Test" },
{ "description", "A basic API test" }
};
string Json = JsonConvert.SerializeObject(BugData, Formatting.Indented);
var Response = await Client.PostAsync("http://bugzillaaddress/rest/bug", new StringContent(Json, Encoding.UTF8, "application/json"));
它似乎生成的 JSON 是:
{
"Bugzilla_api_key": "Removed for security",
"product": "Test",
"component": "Test Component",
"version": "unspecified",
"summary": "Basic API Test",
"description": "A basic API test"
}
知道我做错了什么吗?
完整错误响应:
{StatusCode: 400, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Connection: close
Date: Fri, 29 Jan 2016 13:36:01 GMT
ETag: IzeHlNLRTewC8+btLeGxXA
Server: Microsoft-IIS/8.5
Access-control-allow-headers: origin, content-type, accept, x-requested-with
Access-control-allow-origin: *
X-content-type-options: nosniff
X-frame-options: SAMEORIGIN
X-xss-protection: 1; mode=block
Content-Length: 11
Content-Type: text/html
}}
这是 Fiddler 看到的:
POST http://bugzilla-tools/rest/bug HTTP/1.1 Content-Type:
application/json; charset=utf-8 Connection: Keep-Alive Content-Length:
193 Host: bugzilla-tools
{"Bugzilla_api_key":"Removed for
security","product":"Test","component":"Test
Component","version":"unspecified","summary":"Basic API
Test","description":"A basic API test"}
编辑:Bugzilla 团队的回应
我显然没有在他们要求的请求中传递接受 header。如果我添加一个 accept header 我应该很好。任何人都知道该怎么做? (我现在正在寻找和玩弄一些东西,但如果有人有代码,我可以复制并过去以结束与此 API 的 4 天战斗,那就太好了!)
我成功进入了我的电脑。这是一些示例代码。
var Client = new HttpClient();
Dictionary <string, string> BugData = new Dictionary<string, string>
{
{ "Bugzilla_api_key", "Removed for scurity" },
{ "product", "Test" },
{ "component", "Test Component" },
{ "version", "unspecified" },
{ "summary", "Basic API Test" },
{ "description", "A basic API test" }
};
string Json = JsonConvert.SerializeObject(BugData, Formatting.Indented);
var request = new HttpRequestMessage(HttpMethod.Post, "http://bugzillaaddress/rest/bug");
request.Content = new StringContent(Json, Encoding.UTF8, "application/json")
request.Headers.Add("Accept", "application/json");
var Response = await Client.SendAsync(request);
编辑
我实际上注意到您也可以使用 PostAsync
来做到这一点。
改成这样
var Client = new HttpClient();
Dictionary <string, string> BugData = new Dictionary<string, string>
{
{ "Bugzilla_api_key", "Removed for scurity" },
{ "product", "Test" },
{ "component", "Test Component" },
{ "version", "unspecified" },
{ "summary", "Basic API Test" },
{ "description", "A basic API test" }
};
string Json = JsonConvert.SerializeObject(BugData, Formatting.Indented);
var content = new StringContent(Json, Encoding.UTF8, "application/json");
content.Headers.Add("Accept", "application/json");
var Response = await Client.PostAsync("http://bugzillaaddress/rest/bug", content);
我正在尝试将一些 JSON 格式的数据传递给 Bugzilla API 但我收到了 400
响应。我正在使用 Netwonsoft.Json
生成 JSON,据我所知,它生成的很好,所以我不确定是什么导致了 400 错误。
代码:
var Client = new HttpClient();
Dictionary <string, string> BugData = new Dictionary<string, string>
{
{ "Bugzilla_api_key", "Removed for scurity" },
{ "product", "Test" },
{ "component", "Test Component" },
{ "version", "unspecified" },
{ "summary", "Basic API Test" },
{ "description", "A basic API test" }
};
string Json = JsonConvert.SerializeObject(BugData, Formatting.Indented);
var Response = await Client.PostAsync("http://bugzillaaddress/rest/bug", new StringContent(Json, Encoding.UTF8, "application/json"));
它似乎生成的 JSON 是:
{
"Bugzilla_api_key": "Removed for security",
"product": "Test",
"component": "Test Component",
"version": "unspecified",
"summary": "Basic API Test",
"description": "A basic API test"
}
知道我做错了什么吗?
完整错误响应:
{StatusCode: 400, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Connection: close
Date: Fri, 29 Jan 2016 13:36:01 GMT
ETag: IzeHlNLRTewC8+btLeGxXA
Server: Microsoft-IIS/8.5
Access-control-allow-headers: origin, content-type, accept, x-requested-with
Access-control-allow-origin: *
X-content-type-options: nosniff
X-frame-options: SAMEORIGIN
X-xss-protection: 1; mode=block
Content-Length: 11
Content-Type: text/html
}}
这是 Fiddler 看到的:
POST http://bugzilla-tools/rest/bug HTTP/1.1 Content-Type: application/json; charset=utf-8 Connection: Keep-Alive Content-Length: 193 Host: bugzilla-tools
{"Bugzilla_api_key":"Removed for security","product":"Test","component":"Test Component","version":"unspecified","summary":"Basic API Test","description":"A basic API test"}
编辑:Bugzilla 团队的回应
我显然没有在他们要求的请求中传递接受 header。如果我添加一个 accept header 我应该很好。任何人都知道该怎么做? (我现在正在寻找和玩弄一些东西,但如果有人有代码,我可以复制并过去以结束与此 API 的 4 天战斗,那就太好了!)
我成功进入了我的电脑。这是一些示例代码。
var Client = new HttpClient();
Dictionary <string, string> BugData = new Dictionary<string, string>
{
{ "Bugzilla_api_key", "Removed for scurity" },
{ "product", "Test" },
{ "component", "Test Component" },
{ "version", "unspecified" },
{ "summary", "Basic API Test" },
{ "description", "A basic API test" }
};
string Json = JsonConvert.SerializeObject(BugData, Formatting.Indented);
var request = new HttpRequestMessage(HttpMethod.Post, "http://bugzillaaddress/rest/bug");
request.Content = new StringContent(Json, Encoding.UTF8, "application/json")
request.Headers.Add("Accept", "application/json");
var Response = await Client.SendAsync(request);
编辑
我实际上注意到您也可以使用 PostAsync
来做到这一点。
改成这样
var Client = new HttpClient();
Dictionary <string, string> BugData = new Dictionary<string, string>
{
{ "Bugzilla_api_key", "Removed for scurity" },
{ "product", "Test" },
{ "component", "Test Component" },
{ "version", "unspecified" },
{ "summary", "Basic API Test" },
{ "description", "A basic API test" }
};
string Json = JsonConvert.SerializeObject(BugData, Formatting.Indented);
var content = new StringContent(Json, Encoding.UTF8, "application/json");
content.Headers.Add("Accept", "application/json");
var Response = await Client.PostAsync("http://bugzillaaddress/rest/bug", content);