I am trying to pass some data in JSON format to the Bugzilla API but am getting a 400
response. I am using Netwonsoft.Json
to generate the JSON and it from what I can tell it is generated fine so I am not really sure what is causing the 400 error.
Code:
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"));
The JSON it appears to be generating is:
{
"Bugzilla_api_key": "Removed for security",
"product": "Test",
"component": "Test Component",
"version": "unspecified",
"summary": "Basic API Test",
"description": "A basic API test"
}
Any ideas what I am doing wrong here?
Full Error response:
{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
}}
This is what Fiddler sees:
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"}
Edit: Response from Bugzilla Team
I am apparently not passing an Accept header in with my request which they require. If I add an accept header I should be good. Anyone know how to do that? (I am looking right now and playing with things but if someone has code I can copy and past to end 4 days of fighting with this API that would be great!)
I managed to get onto my PC. Here is some sample code.
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);
Edit
I actually noticed that you can do this using PostAsync
as well.
Change it to this.
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);