Configuring GET Query Parameters and POST Request Bodies in Locust
This guide explains how to use Locust to add query parameters to GET requests and send JSON or form‑encoded data in POST requests, including combined examples with custom headers for comprehensive HTTP request configuration.
In Locust, configuring HTTP request parameters involves passing query parameters for GET requests and request body data for POST requests such as JSON or form‑encoded data.
Configuring GET query parameters – Use the params argument when calling self.client.get . Example:
from locust import HttpUser, task, between
class WebsiteUser(HttpUser):
wait_time = between(1, 5)
@task
def get_with_params(self):
params = {
'param1': 'value1',
'param2': 'value2',
}
self.client.get("/search", params=params)This appends param1 and param2 to the URL, producing a request like http://your-target-host/search?param1=value1¶m2=value2 .
Configuring POST request bodies – Use the json argument for JSON payloads or the data argument for form data. Examples:
@task
def post_with_json_body(self):
payload = {
"title": "foo",
"body": "bar",
"userId": 1,
}
self.client.post("/posts", json=payload) @task
def post_with_form_data(self):
form_data = {
"field1": "value1",
"field2": "value2",
}
self.client.post("/submit-form", data=form_data)Combined example – Setting global headers, sending a GET request with query parameters, and a POST request with a JSON body in the same task:
from locust import HttpUser, task, between
class WebsiteUser(HttpUser):
wait_time = between(1, 5)
def on_start(self):
"""Called before any task is executed"""
self.headers = {
'Authorization': 'Bearer your_access_token',
'Content-Type': 'application/json',
}
@task
def perform_request(self):
# GET with query parameters
params = {
'filter': 'active',
'page': 1,
}
self.client.get("/api/items", params=params, headers=self.headers)
# POST with JSON payload
payload = {
"name": "New Item",
"description": "Description of the new item.",
}
self.client.post("/api/items", json=payload, headers=self.headers)Summary: Use params for GET query strings, json for JSON bodies, data for form data, and you can supply headers alongside params or json in the same request.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.