karate icon indicating copy to clipboard operation
karate copied to clipboard

improve JS API to retrieve request and response

Open ptrthomas opened this issue 2 years ago • 2 comments

refer conversation here: https://github.com/karatelabs/karate/pull/1861#issuecomment-1080562695

proposing the following for a start:

# get single header
* match karate.response.header('foo') == 'bar'

# get all header values by key
* match karate.response.headers('foo') == ['bar']

# same pattern for request
* match karate.request.header('foo') == 'bar'

# in future can add some helpers:
* match karate.response.body == 'success'
* match karate.request.method == 'PUT'

ptrthomas avatar Mar 28 '22 12:03 ptrthomas

also see another use-case: https://stackoverflow.com/a/72245287/143475

ptrthomas avatar May 27 '22 11:05 ptrthomas

cc @fscarlato - this is implemented. here are the new API-s possible:

karate.response.header('some-header') - gets the header value ignoring case karate.response.headerValues('some-header') gets all values for the header (ignoring-case) as a string-array,

the same works for karate.request. the great thing is that it can be used even in mock routing expressions:

Scenario: pathMatches('/v1/headers') && karate.request.header('foo') == 'bar'

ptrthomas avatar Aug 05 '22 07:08 ptrthomas

1.3.0 released

ptrthomas avatar Nov 02 '22 17:11 ptrthomas