Karate - Trouble passing correct headers for authorization

Viewed 120

I am have some problems passing in the correct headers for my graphql endpoints

The use case in Postman:

  • call requestToken endpoint to obtain sessionToken value
  • requestToken response contains Key Value " and Token Value.
  • For subsequent calls, I set postman headers as: Key = X_SESSION_TOKEN Value = Token Value

The user case in Karate

  • 1st feature 'requestToken.feature' successfully calls and stores key + tokenValue

  • 2nd feature successfully defines and prints the token value here is my 2nd request:

    Feature: version

         Background: 
         * url 'http://api-dev.markq.com:5000/'
         * def myFeature = call read('requestToken.feature')
         * def authToken = myFeature.sessionToken
         * configure headers = { 'X_SESSION_TOKEN': authToken , 'Content-Type': 'application/json' }
    
         Scenario: get version
         Given path 'query'
         Given text query = 
         """
         query {
         version
         }
    
         """
    
         And request { query: '#(query)' }
         When method POST
         Then status 200
         And print authToken
         And print response
    

I am not sure I send the headers right. Its coming back 200, but I keep getting a error 'token malformed' in the response message

Any suggestions? New at this, thanks!

1 Answers

Honestly this is hard to answer, a LOT depends on the specific server.

EDIT: most likely it is this change needed, explained here: https://github.com/intuit/karate#embedded-expressions

* configure headers = { 'X_SESSION_TOKEN': '#(authToken)' , 'Content-Type': 'application/json' }

2 things from experience:

  • should it be X-SESSION-TOKEN
  • add an Accept: 'application/json' header

And try to hardcode the headers before attempting call etc.

Here is an example that works for me:

* url 'https://graphqlzero.almansi.me/api'
* text query =
"""
{
  user(id: 1) {
    posts {
      data {
        id
        title
      }
    }
  }
}
"""
* request { query: '#(query)' }
* method post
* status 200
Related