Skip to main content

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.

Core Keywords

  • url - Set the base URL for HTTP requests
  • path - Build REST-style URL paths with automatic encoding
  • request - Set the request body (JSON, XML, or text)
  • method - Execute HTTP requests (GET, POST, PUT, DELETE, etc.)
  • status - Assert HTTP response status codes
  • soap action - Handle SOAP web services

Keywords

url - Set Base URL

Define the base URL for your HTTP requests. The URL persists across multiple requests until explicitly changed with another url statement:

Gherkin
Feature: URL handling

Scenario: Basic URL setup
Given url 'https://jsonplaceholder.typicode.com'
And path 'users'
When method get
Then status 200
Environment-Specific URLs

Use variables from karate-config.js for environment-specific URLs. Define baseUrl or similar in your config, then reference it:

Given url baseUrl
And path 'users'

See Configuration for setting up environment-specific URLs.

Using Configuration Variables

Define environment-specific URLs in karate-config.js and reference them as variables. This is the recommended approach for managing URLs across dev, staging, and production environments:

karate-config.js
function fn() {
var env = karate.env || 'dev';
var config = {
baseUrl: 'https://jsonplaceholder.typicode.com'
};

if (env == 'prod') {
config.baseUrl = 'https://jsonplaceholder.typicode.com';
} else if (env == 'stage') {
config.baseUrl = 'https://jsonplaceholder.typicode.com';
}

return config;
}
Gherkin
Feature: Using config variables

Scenario: Reference baseUrl from config
Given url baseUrl
And path 'users'
When method get
Then status 200

path - URL Path Construction

Build REST-style URL paths with automatic URL encoding:

Gherkin
Feature: Path construction

Scenario: Simple path
Given url 'https://jsonplaceholder.typicode.com'
And path 'users'
When method get
Then status 200
# Result: https://jsonplaceholder.typicode.com/users

Scenario: Path with variables
Given url 'https://jsonplaceholder.typicode.com'
* def userId = 1
And path 'users', userId
When method get
Then status 200
And match response.id == 1
# Result: https://jsonplaceholder.typicode.com/users/1

Multiple Path Segments

Combine multiple path segments using comma-separated values or multiple path statements:

Gherkin
Feature: Complex paths

Background:
* url 'https://jsonplaceholder.typicode.com'
* def userId = 1

Scenario: Comma-delimited path segments
Given path 'users', userId, 'posts'
When method get
Then status 200

Scenario: Multiple path statements
Given path 'posts'
And path userId
And path 'comments'
When method get
Then status 200
Query Strings in Path

Do not use path to add query strings. The ? character will be URL-encoded to %3F.

Incorrect approach:

* path 'users?status=active'  # Results in /users%3Fstatus=active

Correct approach:

* path 'users'
* param status = 'active' # Results in /users?status=active

Alternative - 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:

Gherkin
Feature: Request body handling

Scenario: JSON request body
Given url 'https://jsonplaceholder.typicode.com'
And path 'posts'
And request { title: 'My Post', body: 'Post content', userId: 1 }
When method post
Then status 201
And match response.id == '#number'

Scenario: XML request body
Given url 'https://httpbin.org'
And path 'post'
And request <user><name>John</name><email>john@test.com</email></user>
When method post
Then status 200

Request from Files

Load request bodies from external files using the read() function for better organization and reusability:

Gherkin
Feature: File-based requests

Scenario: Load request body from file
Given url 'https://jsonplaceholder.typicode.com'
And path 'posts'
And request read('classpath:requests/createPost.json')
When method post
Then status 201

Scenario: Empty request body
Given url 'https://httpbin.org'
And path 'get'
And request ''
When method get
Then status 200

method - HTTP Verbs

Execute HTTP requests using standard HTTP methods:

Gherkin
Feature: HTTP methods

Background:
* url 'https://jsonplaceholder.typicode.com'

Scenario: GET request
Given path 'users', 1
When method get
Then status 200
And match response.id == 1

Scenario: POST request
Given path 'posts'
And request { title: 'My Post', body: 'Content', userId: 1 }
When method post
Then status 201

Scenario: PUT request
Given path 'posts', 1
And request { id: 1, title: 'Updated Title', body: 'Updated', userId: 1 }
When method put
Then status 200
Method Execution Order

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://jsonplaceholder.typicode.com'
* path 'users'
* header Accept = 'application/json'
* method get

Incorrect - header set after request:

* url 'https://jsonplaceholder.typicode.com'
* path 'users'
* method get
* header Accept = 'application/json' # Too late!

The method step triggers the HTTP call immediately. Any setup after method will not affect that request.

Other HTTP Methods

Karate supports all standard HTTP methods including DELETE, PATCH, OPTIONS, and HEAD:

Gherkin
Feature: Additional HTTP methods

Background:
* url 'https://jsonplaceholder.typicode.com'

Scenario: DELETE request
Given path 'posts', 1
When method delete
Then status 200

Scenario: PATCH request
Given path 'posts', 1
And request { title: 'Patched Title' }
When method patch
Then status 200

Scenario: Dynamic method selection
* def httpMethod = 'get'
Given path 'users', 1
When method httpMethod
Then status 200

status - Response Code Validation

Assert HTTP response status codes:

Gherkin
Feature: Status code validation

Background:
* url 'https://jsonplaceholder.typicode.com'

Scenario: Success responses
Given path 'users'
When method get
Then status 200

Scenario: Created response
Given path 'posts'
And request { title: 'New Post', body: 'Content', userId: 1 }
When method post
Then status 201

Scenario: Not Found
Given path 'users', 99999
When method get
Then status 404

SOAP Support

soap action - SOAP Requests

Handle SOAP web services using soap action instead of method:

Gherkin
Feature: SOAP web services

Scenario: SOAP request with inline XML
Given url soapServiceUrl
And request
"""
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetUser xmlns="http://example.com/users">
<UserId>1</UserId>
</GetUser>
</soap:Body>
</soap:Envelope>
"""
When soap action 'GetUser'
Then status 200
And match response/Envelope/Body/GetUserResponse/User/Name == '#string'

SOAP from Files

Store SOAP XML envelopes in external files for cleaner test code and easier maintenance:

Gherkin
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

Combine all request components (URL, path, headers, and body) to build production-ready API test scenarios:

Gherkin
Feature: Complete HTTP request

Scenario: Create user with full request setup
Given url 'https://jsonplaceholder.typicode.com'
And path 'users'
And header Accept = 'application/json'
And request
"""
{
name: 'John Doe',
username: 'johndoe',
email: 'john@example.com',
address: {
street: '123 Main St',
city: 'Springfield',
zipcode: '12345'
},
phone: '555-1234'
}
"""
When method post
Then status 201
And match response.id == '#number'
And match response.name == 'John Doe'
And match response.address.city == 'Springfield'

Request Lifecycle

URL Persistence and Reset

The url keyword persists across scenarios when set in Background, but path resets after each HTTP request. This behavior allows you to set the base URL once and reuse it with different paths:

Gherkin
Feature: URL lifecycle

Background:
Given url 'https://jsonplaceholder.typicode.com'

Scenario: URL persists, path resets
And path 'users'
When method get
Then status 200
# Path resets after request, URL remains
And path 'posts'
When method get
Then status 200

Scenario: URL still available from Background
And path 'todos'
When method get
Then status 200
Common Gotchas
  • 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 of method for SOAP requests

Next Steps

Master HTTP request creation and continue with: