Search code examples
karategherkinweb-api-testing

Karate - Unable to use a feature response in another request


I'm unable to use the response of one call to prepare a new payload. Same error is coming in preparing a header from auth-feature response. Below is the example for header.

Background:

  * url baseUrl 
  * def resp = call read('classpath:auth-token.feature') { username: 'xxx', password: 'xxx' }
 
  * def token = resp.authToken
  * print 'token is ' , token

  * def authHeader = { Authorization: Bearer '#(token)' }
  * print 'header is ' ,authHeader

First print works fine but the second is unable to resolve.

23:54:09.217 [main] INFO  com.intuit.karate - [print] token is  xxxxxx.eyJzdWIiOiJUTVVQQFRNVVAiLCJvYm8iOiJmYWxzZSIsImV4cCI6MTY0NjQ1NjY0OX0.xxxxxx
23:54:09.222 [main] INFO  com.intuit.karate - [print] header is  {
  "Authorization": "Bearer '#(token)'"
}

The documentation says

if a string value within a JSON (or XML) object declaration is enclosed between #( and ) - it will be evaluated as a JavaScript expression.


Solution

  • Please read this part of the docs: https://github.com/karatelabs/karate#rules-for-embedded-expressions

    Try this:

    * def temp = 'Bearer ' + resp.authToken
    * def authHeader = { Authorization: '#(temp)' }