HTTP RESPONSES
Response Handling
Access HTTP response data using built-in variables for status, headers, cookies, and timing—automatically populated after every request without manual parsing.
On this page:
- response - Access JSON, XML, or text response body
- responseStatus - HTTP status code as integer
- responseHeaders - Headers as map of lists
- responseCookies - Cookies with value, domain, path
- responseTime - Response time in milliseconds
- responseBytes - Raw binary content
- responseType - Parsed type: json, xml, or string
Response Body
After every HTTP call, the response variable is set with the response body and remains available until the next HTTP request overwrites it. The response is automatically parsed as JSON, XML, or String based on content type.
Feature: JSON response access
Scenario: Access response fields
Given url 'https://jsonplaceholder.typicode.com'
And path 'users', 1
When method get
Then status 200
* def userName = response.name
* def userEmail = response.email
* match userName == '#string'
JsonPath Shortcuts
Use $ as a shortcut for the response variable in JsonPath expressions:
Feature: JsonPath shortcuts
Scenario: Shorthand response access
Given url 'https://jsonplaceholder.typicode.com'
And path 'users', 1
When method get
Then status 200
# These three lines are equivalent
* match response.name == '#string'
* match response $.name == '#string'
* match $.name == '#string'
The $ shortcut only works on the response variable. For other variables, use the $varName.path form:
* def user = { name: 'John', age: 30 }
* match $user.name == 'John'
Response Status
Access and validate HTTP status codes using responseStatus. You would normally use the status keyword for simple assertions, but responseStatus is useful for expressions and conditional logic.
Feature: Status code handling
Scenario: Status validation
Given url 'https://jsonplaceholder.typicode.com'
And path 'users', 1
When method get
Then status 200
* def statusCode = responseStatus
* assert statusCode == 200
# Check if status is one of multiple expected values
* match [200, 201, 204] contains responseStatus
Status Range Validation
Use assert for numeric comparisons on status codes:
Feature: Status range checks
Scenario: Check status ranges
Given url 'https://jsonplaceholder.typicode.com'
And path 'users', 1
When method get
# Check if status is in 2xx range
* assert responseStatus >= 200
* assert responseStatus < 300
# Or use karate.range() for a cleaner approach
* match karate.range(200, 299) contains responseStatus
Response Headers
Access response headers via the responseHeaders variable. Note that HTTP headers are a "map of lists" (each header can have multiple values), so you need to use index [0] to get the first value.
Feature: Header access
Scenario: Read response headers
Given url 'https://httpbin.org'
And path 'get'
When method get
Then status 200
* def contentType = responseHeaders['Content-Type'][0]
* match contentType contains 'application/json'
Case-Insensitive Header Access
Use karate.response.header() for case-insensitive header lookup (recommended):
Feature: Case-insensitive headers
Scenario: Read headers ignoring case
Given url 'https://httpbin.org'
And path 'get'
When method get
Then status 200
# Case-insensitive - 'content-type' or 'Content-Type' both work
* match karate.response.header('content-type') contains 'application/json'
- Use
karate.response.header('name')for case-insensitive single-value access (recommended) - Use
responseHeaders['Name'][0]for case-sensitive access with index - Use
match headerkeyword for simple header assertions:And match header Content-Type contains 'json'
Response Cookies
The responseCookies variable is set upon any HTTP response and is a map-like object. Each cookie has properties: value, domain, and path.
Feature: Cookie handling
Scenario: Extract and validate cookies
Given url 'https://httpbin.org'
And path 'cookies', 'set', 'sessionid', 'abc123'
When method get
Then status 200
* match responseCookies contains { sessionid: '#notnull' }
* def sessionValue = responseCookies.sessionid.value
* match sessionValue == 'abc123'
Cookie Properties
Access individual cookie properties for detailed validation:
Feature: Cookie properties
Scenario: Validate cookie attributes
Given url 'https://httpbin.org'
And path 'cookies', 'set', 'testcookie', 'testvalue'
When method get
Then status 200
* def cookie = responseCookies.testcookie
* match cookie.value == 'testvalue'
* karate.log('Cookie domain:', cookie.domain)
* karate.log('Cookie path:', cookie.path)
Cookies from responses are automatically sent with subsequent requests, simulating browser behavior. To disable this, use configure cookies = null.
Response Time
The responseTime variable contains the response time in milliseconds for the current response:
Feature: Performance monitoring
Scenario: Response time validation
Given url 'https://jsonplaceholder.typicode.com'
And path 'users', 1
When method get
Then status 200
And assert responseTime < 2000
* karate.log('Response time:', responseTime, 'ms')
Request Timestamp
For advanced timing analysis, use requestTimeStamp to get the Java system time when the request was initiated:
Feature: Request timestamp tracking
Scenario: Track request timing
Given url 'https://jsonplaceholder.typicode.com'
And path 'users', 1
When method get
Then status 200
* karate.log('Request initiated at:', requestTimeStamp)
* karate.log('Response time:', responseTime, 'ms')
Binary Response Data
The responseBytes variable always holds the response as a byte array, useful for binary content like files, images, or PDFs:
Feature: Binary handling
Scenario: Download and validate binary file
Given url 'https://httpbin.org'
And path 'bytes', 1024
When method get
Then status 200
* def fileSize = responseBytes.length
* assert fileSize == 1024
* karate.log('Downloaded', fileSize, 'bytes')
Save Binary Files
Write binary responses to disk using karate.write():
Feature: File download
Scenario: Save downloaded file
Given url 'https://httpbin.org'
And path 'image', 'png'
When method get
Then status 200
* karate.write(responseBytes, 'target/downloaded-image.png')
* match karate.response.header('Content-Type') == 'image/png'
Response Type Detection
The responseType variable indicates how Karate parsed the response body: json, xml, or string. Use this for dynamic handling of different response formats.
Feature: Content type detection
Scenario: Check response type
Given url 'https://jsonplaceholder.typicode.com'
And path 'users', 1
When method get
Then status 200
And match responseType == 'json'
* match response == '#object'
XML Response Handling
For XML responses, use XPath expressions. The / shortcut represents the response (like $ for JSON):
Feature: XML response access
Scenario: XPath shortcuts
Given url 'https://httpbin.org'
And path 'xml'
When method get
Then status 200
And match responseType == 'xml'
# These are equivalent
* match response /slideshow/slide[1]/title == 'Wake up to WonderWidgets!'
* match /slideshow/slide[1]/title == 'Wake up to WonderWidgets!'
Complete Example
Access all response variables together for comprehensive logging:
Feature: Complete response context
Scenario: Access all response variables
Given url 'https://jsonplaceholder.typicode.com'
And path 'users', 1
When method get
Then status 200
* karate.log('Status:', responseStatus)
* karate.log('Type:', responseType)
* karate.log('Time:', responseTime, 'ms')
* def headerCount = karate.sizeOf(responseHeaders)
* karate.log('Header count:', headerCount)
- Response variables are overwritten after each HTTP request
- Store values in custom variables if you need them after subsequent requests
- Cookies from
responseCookiesare automatically sent in future requests - Check
responseTypeto determine how Karate parsed the response body
Next Steps
- Validate response data: Response Validation
- Monitor performance: Response Time
- Learn advanced matching: Match Keyword