2

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!)

10
  • 1) Remove the indenting, it is only taking up extra data space. 2) Can you show us the exact error message, maybe using something like Fiddler will help. Jan 28, 2016 at 22:33
  • @ShaunSharples I have edited the post to include the full response I get when running the PostAsync command. Jan 29, 2016 at 13:38
  • I think I am out of ideas. In theory, what you have looks correct, even after confirming with the BugZilla API documentation and your JSON in the question is properly formatted. I would suggest maybe try using RestSharp RestClient to rule out any weird behaviour from the HttpClient. Also you could test using Fiddler, and putting your JSON manually in the Request body. Jan 29, 2016 at 20:36
  • @ShaunSharples Unfortunately RestSharp does not support the new ASP.Net 5 Core. I will look into this Fillder thing Jan 29, 2016 at 21:02
  • @ShaunSharples I updated the post with what I found in fiddler. Jan 29, 2016 at 21:21

1 Answer 1

1

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);
2
  • I am not sure if you edit actually works as when I step though my task it kicks back up to the calling action as soon as it processes the content.headers.add line and never gets to the PostAsync line Feb 1, 2016 at 13:37
  • I am accepting this answer as it showed how to pass the Accept header which allowed me to move onto the next error. As a note I thought it was not fixed initially since IIS was not returning the json error info to IE but after using a program called Postman I was able to get the json error information. Feb 1, 2016 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.