'Karate - Setting global request headers

So I've managed to write a bunch of tests and in every feature file I set the same request headers.

For example:

Given url appUrl
And path '/path'
* header Accept = 'application/json'

I'd like to know if there's a way to set a header once so that it is set before each scenario is run. I've read the documentation and tried the callSingle method as follows in karate-config.js:

karate.callSingle('classpath:api/Utilities/Feature/header.feature');

header.feature looks like:

Feature: common routing that sets the headers for all features

  Background:
    * configure headers = { Accept : 'application/json' }

And example feature where I expect the headers to be preset:

 Feature: Header Preset

      Scenario: I expect the header to be set
        Given url appUrl
        And path '/path'
        When method get
        Then status 200
        * print response
       #I expect the response to be returned in JSON format 

However I'm unable to get this working. I don't think I've understood how the callSingle method works. Some pointers would be helpful. Thanks.



Solution 1:[1]

Ignore callSingle for now and focus on configure headers.

I think you are missing one step - which is to ensure that configure headers has been "applied" before each Scenario. If you are 100% sure that this applies "globally", just do this in karate-config.js:

karate.configure('headers', { Accept: 'application/json' });

Else you use the Background (in each feature):

* configure headers = { Accept: 'application/json' }

Typically you have more steps that are common, so you have them in a "common" feature file and call that for every test. Refer: https://github.com/intuit/karate#shared-scope

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Thomas