Search code examples
requesthttp-headerskarate

karate request header validation


Would like to be able to assertions to validate that the request header set matches as expected to help validate tests are correctly setting content types supported.

Checked documentation, github issues, stackoverflow, google

Given url myUrl
And params myParams
And headers myHeaders
And header Authorization = babylonAuth
And request historyPostPayload
When method POST
Then status 200

* match requestHeaders.contains({'Content-Type': ''})

Any way to do this now, or is this a feature request?


Solution

  • Ah, the request header, a very rare need. Yes this is possible by referring to the karate.prevRequest, documented here: https://github.com/intuit/karate#karate-prevrequest

    So this should be what you are looking for:

    * def contentType = karate.prevRequest.headers['Content-Type'][0]
    * match contentType contains 'application/json'
    

    Have a look at this example: encoding.feature