How to handle requests with signatures on karate tests?

Karate has a “hook” for generating headers, but as of now it is not “aware” of the currently built request body + headers: https://github.com/intuit/karate#configure-headers

We got a similar request here, and are thinking of adding this capability: How to retrieve raw request contents before making a REST call in Karate DSL?

Maybe the OAuth examples will give you the way forward for your case for now: https://stackoverflow.com/a/55055111/143475

Feel free to raise an enhancement request, and we can get this in to the next version (with your help to test it). I’m thinking – what if you are able to call karate.get('request') from within the header JS function.

But for now all you need to do is do something like this:

* def body = { some: 'json' }
* karate.set('requestBody', body)
* url someUrl
* request body
* method post

And in the header.js function

function fn() {
  var body = karate.get('requestBody');
  var sign = Utils.sign(body);
  return { Signature: sign };  
}

EDIT: this will be implemented in Karate 1.0 onwards: https://github.com/intuit/karate/issues/1385

Leave a Comment