HTTP REQUESTS
Making Requests
Build and execute HTTP requests using Karate's core keywords (url, path, request, method, status) without writing custom step definitions or Java code.
Benefits of Core Keywords
- No glue code: Built-in keywords eliminate Java step definitions
- URL reusability: Set base URL once, reuse across multiple requests
- Clear lifecycle: Understand when URLs persist and when paths reset
- SOAP support: Handle both REST and SOAP requests with the same framework
Core Keywords
url - Set Base URL
Set the base URL for HTTP requests:
Feature: URL handling
Scenario: Basic URL setup
Given url 'https://api.example.com'
And path 'users'
When method get
Then status 200
- URL remains constant until you use the
url
keyword again - Use variables from
karate-config.js
for environment-specific URLs - URL persists across scenarios when defined in Background
Dynamic URL Construction
Feature: Dynamic URLs
Scenario: Build URLs with variables
* def apiHost = 'api.example.com'
* def version = 'v2'
Given url 'https://' + apiHost + '/' + version
And path 'users'
When method get
Then status 200
Scenario: Environment-specific URLs
* def env = karate.env || 'dev'
* def envUrl = env == 'prod' ? 'https://api.example.com' : 'https://api-' + env + '.example.com'
Given url envUrl
And path 'users'
When method get
Then status 200
path - URL Path Construction
Build REST-style URL paths with automatic URL encoding:
Feature: Path construction
Scenario: Simple path
Given url 'https://api.example.com'
And path 'users'
When method get
Then status 200
# Result: https://api.example.com/users
Scenario: Path with variables
Given url 'https://api.example.com'
* def userId = 123
And path 'users', userId
When method get
Then status 200
# Result: https://api.example.com/users/123
Multiple Path Segments
Feature: Complex paths
Scenario: Comma-delimited path segments
Given url 'https://api.example.com'
* def userId = 123
And path 'users', userId, 'profile', 'settings'
When method get
Then status 200
# Result: https://api.example.com/users/123/profile/settings
Scenario: Multiple path statements
Given url 'https://api.example.com'
And path 'api'
And path 'v2'
And path 'users'
When method get
Then status 200
# Result: https://api.example.com/api/v2/users
Do not use path
to add query strings. The ?
character will be URL-encoded to %3F
.
❌ Wrong:
* path 'users?status=active' # Results in /users%3Fstatus=active
✅ Correct:
* path 'users'
* param status = 'active' # Results in /users?status=active
Or use url
directly:
* url baseUrl + '/users?status=active'
See Request Parameters for query string handling.
request - Request Body
Set the request body for HTTP methods:
Feature: Request body handling
Scenario: JSON request body
Given url baseUrl
And path 'users'
And request { name: 'John Doe', email: 'john@example.com' }
When method post
Then status 201
Scenario: XML request body
Given url baseUrl
And path 'users'
And request <user><name>John</name><email>john@test.com</email></user>
When method post
Then status 200
Request from Files
Feature: File-based requests
Scenario: Load request body from file
Given url baseUrl
And path 'users/import'
And request read('bulk-users.json')
When method post
Then status 202
Scenario: Empty request body
Given url baseUrl
And path 'ping'
And request ''
When method get
Then status 200
method - HTTP Verbs
Execute HTTP requests using standard HTTP methods:
Feature: HTTP methods
Scenario: GET request
Given url baseUrl
And path 'users', 123
When method get
Then status 200
Scenario: POST request
Given url baseUrl
And path 'users'
And request { name: 'John', email: 'john@test.com' }
When method post
Then status 201
Scenario: PUT request
Given url baseUrl
And path 'users', 123
And request { name: 'John Updated', email: 'john@test.com' }
When method put
Then status 200
The HTTP request is issued when the method
keyword is executed. Complete all setup (url
, path
, header
, param
, configure
) before calling method
.
✅ Correct order:
* url 'https://api.example.com'
* path 'users'
* header Authorization = 'Bearer ' + token
* param page = 1
* method get
❌ Wrong - header set after request:
* url 'https://api.example.com'
* path 'users'
* method get
* header Authorization = 'Bearer ' + token # Too late!
The method
step triggers the HTTP call immediately. Any setup after method
will not affect that request.
Other HTTP Methods
Feature: Additional HTTP methods
Scenario: DELETE request
Given url baseUrl
And path 'users', 123
When method delete
Then status 204
Scenario: PATCH request
Given url baseUrl
And path 'users', 123
And request { name: 'Patched Name' }
When method patch
Then status 200
Scenario: Dynamic method selection
Given url baseUrl
* def httpMethod = 'patch'
And path 'users', 123
And request { name: 'Patched Name' }
When method httpMethod
Then status 200
status - Response Code Validation
Assert HTTP response status codes:
Feature: Status code validation
Scenario: Success responses
Given url baseUrl
And path 'users'
When method get
Then status 200
Scenario: Created response
Given url baseUrl
And path 'users'
And request { name: 'John' }
When method post
Then status 201
Error Status Codes
Feature: Error responses
Scenario: Not Found
Given url baseUrl
And path 'users', 99999
When method get
Then status 404
Scenario: Bad Request
Given url baseUrl
And path 'users'
And request { invalidField: 'value' }
When method post
Then status 400
SOAP Support
soap action - SOAP Requests
Handle SOAP web services using soap action
instead of method
:
Feature: SOAP web services
Scenario: SOAP request with inline XML
Given url 'https://soap.example.com/services'
And request
"""
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<QueryUser xmlns="http://example.com/users">
<UserId>12345</UserId>
</QueryUser>
</soap:Body>
</soap:Envelope>
"""
When soap action 'QueryUser'
Then status 200
And match response/Envelope/Body/QueryUserResponse/User/Name == 'John Doe'
SOAP from Files
Feature: File-based SOAP requests
Scenario: Load SOAP request from file
Given url soapUrl
And request read('soap-request.xml')
When soap action 'GetBalance'
Then status 200
And match response /Envelope/Body/GetBalanceResponse/Balance == '#number'
Request Building Patterns
Complete Request Flow
Build a complete HTTP request step by step:
Feature: Complete HTTP request
Scenario: Build complex API request
Given url 'https://api.example.com'
And path 'api', 'v2', 'users'
And param limit = 10
And param sort = 'name'
And header Authorization = 'Bearer ' + authToken
And header Accept = 'application/json'
And request { name: 'John Doe', email: 'john@example.com' }
When method post
Then status 201
And match response.id == '#number'
And match response.name == 'John Doe'
Complex Request Body
Feature: Nested request body
Scenario: Request with nested JSON
Given url baseUrl
And path 'users'
And request
"""
{
name: 'John Doe',
email: 'john@example.com',
profile: {
firstName: 'John',
lastName: 'Doe',
preferences: ['email', 'sms']
}
}
"""
When method post
Then status 201
Dynamic Request Construction
Build requests dynamically based on environment or conditions:
Feature: Dynamic request building
Scenario: Environment-based URL
* def endpoint = karate.env == 'prod' ? 'api.example.com' : 'api-dev.example.com'
* def version = 'v2'
Given url 'https://' + endpoint + '/' + version
And path 'users'
When method get
Then status 200
Scenario: Conditional request body
Given url baseUrl
And path 'users'
* def operation = 'create'
* def requestBody = operation == 'create' ? { name: 'New User', email: 'new@test.com' } : { name: 'Updated User' }
And request requestBody
When method post
Then status 201
Request Lifecycle
URL Persistence and Reset
Understand how URL and path behave across scenarios:
Feature: URL lifecycle
Background:
Given url 'https://api.example.com'
Scenario: URL persists, path resets
And path 'users'
When method get
Then status 200
# Path resets after request, URL remains
And path 'products'
When method get
Then status 200
Scenario: URL still available from Background
And path 'orders'
When method get
Then status 200
Configure URL for Feature Calls
Use configure url
to persist URL across feature calls:
Feature: URL in feature calls
Background:
* configure url = 'https://api.example.com'
Scenario: URL persists with feature calls
And path 'auth/login'
And request { username: 'test', password: 'secret' }
When method post
Then status 200
* call read('user-operations.feature')
# URL still available after feature call
And path 'logout'
When method post
Then status 200
- Path resets: Path is reset after each HTTP request, but URL persists
- Method triggers request: The
method
keyword actually sends the HTTP request - Status assertion:
status
keyword causes test failure if code does not match - SOAP vs REST: Use
soap action
instead ofmethod
for SOAP requests
Next Steps
Master HTTP request creation and continue with:
- Add query parameters and form data: Request Parameters
- Manage HTTP headers and cookies: Headers and Cookies
- Handle multipart file uploads: Multipart Requests
- Configure request settings and SSL: Configuration