Join 34,000+ subscribers and receive articles from our blog about software quality, testing, QA and security.
 

POST request, 400 Bad Request Error - Content-Type header missing


#1

Now I’m developing some automation framework integration TestRail using JAVA code. But I faced below issue, could you please kindly help? Thank you.

Pre-condition: Using the official binding for JAVA which named APIClient.java.
I can get expected response for ‘sendGet’.

Issue: When I want to use ‘sendPost’ request, there is 400 Error as “Content-Type header missing (use Content-Type: application/json)”, the source code is below:

          Map data = new HashMap();
          data.put("description", "A description for the test run");
          JSONObject r = (JSONObject) client.sendPost("update_run/54859", data);
          log.info(r.toJSONString());

=================================

And I also try other command like add_run, add_results and all such commands get this error, but I can get successfully response for ‘sendGet’ using the same credential.

I also checked the source code in APIClient.java there is code: conn.addRequestProperty(“Content-Type”, “application/json”) to set Header.

What’s more, I used the Postman tool to send Post request also get the same error, the request is ‘https://adcl-testrail.amazon.com/testrail/api/v2/update_run/54859’ and body is
{
“description”:“A description for the test run”
}


#2

for me in Posteman following works:
POST: https://firm.testrail.io/index.php?/api/v2/update_run/73

  • set in tab Authorization: “Basic Auth” + Username: your@email.com + Password: your password
  • add in tab Header: key|Value: “Content-Type”|“application/json”
  • add in tab body: (using raw)
    {
    “description”: “A description for the test run”
    }