Alex D
Alex D

Reputation: 410

How can I centrally set a `content-type` in Karate without it being overwritten to 'application/json' when there's a request body?

I have a * configure headers = read('classpath:configure-headers.js') in the Background of each of my feature files.

The configure-header.js looks like:

function() {
    var out = {'Some-Header-We-Need': 'value'};
    var authToken = karate.get('authToken');
    if(authToken) {
        out['Authorization'] = 'Bearer ' + authToken;
        out['Content-Type'] = 'application/vnd.mycompany+json';
    }
    return out;
}

These headers always appear where I expect, except that when I'm making a PUT or PATCH or POST, the Content-Type header is being set to application/json. I can get my desired header by setting it before the call, e.g.

Given path myPath
And header Content-Type = 'application/vnd.mycompany+json'
And request read('classpath:requestBody.json')
When method POST

What can I do to not need to rewrite this header everywhere?

Upvotes: 1

Views: 447

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

Wow, you actually did surface a bug in Karate that went un-detected for a long time. Thanks !

I've opened an issue here, and the fix is in the develop branch: https://github.com/intuit/karate/issues/510

Hopefully you can manage for a little while with the work-around. If urgent we can release a patch version.

Upvotes: 1

Related Questions