How do I set header variables in Javascript when the API names have dashes in them?

Viewed 981

New to Karate. Rusty at javascript. I've read through the intuit.karate page. I'm sending out the S.O.S. here, please be kind. How would I configure headers via .js when the headers have names like Content-Type? I am not referring to the value but the actual name. I have a header api with a mix of names that contain a dash and some that don't. In addition, can you update a header after a configure but before the actual call. Trying something like this which is failing miserably:

''' Background:

* callonce read('getAuthorization.feature')
* configure headers = read('classpath:payload.json')
* print 'headers: ', headers
* configure headers = {"Authorization" : authToken}

'''

Where payload.json has static values and Authorization authToken comes from the getAuthorization.feature. I want the Authorization to be added to the header info from payload.json.

I apologize in advance for any confusion in this question. I tried the intuit karate page and examples and youtube vids before coming here.

1 Answers

Few changes you can consider

  • When header has a dash in them like Content-Type
var header = {}
header["Content-Type"] = "<CONTENT_TYPE>"
  • Embedded expression to inject value directly to payload.json when you read

payload.json

{
  "Authorization" : "#(authToken)"
  "Content-Type"  : "application/json"
}

In the feature file:

* callonce read('getAuthorization.feature')
* configure headers = read('classpath:payload.json')

above 2 steps should be enough.

  • header using js

header.js

function fn() {
    var authCall = karate.callSingle('getAuthorization.feature')
    var headers = {}
    // add all static headers as well
    headers["Content-Type"] = "application/json"
    headers["Authorization"] = authCall.authToken
    return headers
}

In feature:

* configure headers = read('classpath:header.js')()

or

* configure headers = call read('classpath:header.js')
Related