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

Content-Type header invalid, use Content-Type: application\/json


#1

Hi,

I’m writing a POST code in Google Sheets script that will go through a test run export populated with results and push it into TR.
My code is as follows:
[…]
function constructApi() {
var headers =
{
‘Content-Type’: ‘application/json’,
‘Authorization’: ‘Basic ’ + Utilities.base64Encode(’’ + ‘:’ + ‘’),
};
var data =
{
“status_id”:“1”,
“comments”:“Just testing stuff”,
“assignedto_id”:“1”,
};
var payload = JSON.stringify(data);

//also method and specifying the headers above
var options =
{
“method”:“POST”,
“headers” : headers,
“payload” : payload,
};

var runId = resolveRunId();
//Logger.log(options.results[0].case_id);
var response = UrlFetchApp.fetch(“https://<>.testrail.net/index.php?/api/v2/add_result_for_case/94/341”, options);

[…]

but I’m getting the following -
[…]
Request failed for https://<>.testrail.net/index.php?/api/v2/add_result_for_case/94/341 returned code 400. Truncated server response: {“error”:“Content-Type header invalid (use Content-Type: application/json)”} (use muteHttpExceptions option to examine full response) (line 46, file “submitToTestRail”)
[…]

Content-Type is in fact application/json so it’s not clear what’s the problem. The same method, only with GET, is used in another script and it’s running just fine.

Thanks.


#2

Hello Assaf,

Thanks for your posting. Could you please try to use the following instead?

‘Content-Type’: ‘application/json’,

(note the missing backslash)

Also please double-check that you really use https instead of http. We have seen this issue before when only http was used (due to headers not being resent after the redirect to https).

Regards,
Tobias


#3

Hi Tobias,

I tried w/o the ‘’ and still without success. https:// is also in place.
The above is an exact copy-paste (minus the sensitive information).

Thanks again.


#4

Thanks, Assaf. Do you have the option to take a look at the raw HTTP request that is sent? It could be that the UrlFetchApp API handles POST requests differently than GETs and does not include the Content-Type header or something else. If you have the option to review the actual HTTP request that is really sent, that would make it easier to troubleshoot.

Regards,
Tobias


#5

URLFetchApp doesn’t support “Content-Type” headers for GET requests. This is correct, as the Content-Type header denotes the content type of the request body, which is unsupported for GET. The closest “correct” header would be “Accept” for the expected content type of the response body.

TestRail needs to remove the requirement or also allow the header in “Accept”


#6

I’m having the same problem. Content-Type is not a valid header for get requests and several http libraries forbid it. After 2 years, can we get this check removed? It’s putting a hold to several integration projects we are working on.

Referencing another case: Making an API GET request without the header Content-Type


#7

Hi everyone,

We are happy to look into this, thanks for your feedback!

Cheers,
Tobias


#8

Hi ,

I have a similar issue :

When I send get its works with the content type :

But when I send post,I get below error:

{
“error”: “Content-Type header invalid (use Content-Type: application/json)”
}

Please help!

BR//
Priti


#9

Hi TestRail Team,

Could you please look into my issues.

Thanks!

BR//
Priti


#10

Just came across this issue as well. A bit of a bummer since it’s blocking our CI implementation. Any workarounds?