How to Configure HTTP Request Headers in Locust

How to Configure HTTP Request Headers in Locust

Configuring HTTP request headers in Locust is very simple. You can achieve this by passing the headers parameter when calling the appropriate method of the client object. Below, we will detail how to add custom request headers for different types of HTTP requests.

How to Configure HTTP Request Headers in Locust

How to Set Request Headers for HTTP Requests in Locust

1. Basic Setup

Assuming we need to make a GET request to an API and set specific request headers (such as an authorization token or content type), we can do it like this:

from locust import HttpUser, task, between

class WebsiteUser(HttpUser):
    wait_time = between(1, 5)  # Set user wait time

    @task
    def get_with_custom_header(self):
        headers = {
            'Authorization': 'Bearer your_access_token',
            'Content-Type': 'application/json'
        }
        self.client.get("/api/resource", headers=headers)

In this example, we define a class named WebsiteUser that inherits from HttpUser. In the get_with_custom_header task, we create a dictionary headers containing two header values, and then send a GET request using the self.client.get() method, passing headers as a parameter.

2. Setting Request Headers for POST Requests

For POST requests, you can also pass the headers parameter when calling the self.client.post() method. Additionally, you can specify the data to be sent (usually in JSON format):

@task
def post_with_custom_header(self):
    headers = {
        'Authorization': 'Bearer your_access_token',
        'Content-Type': 'application/json'
    }
    payload = {
        "title": "foo",
        "body": "bar",
        "userId": 1,
    }
    self.client.post("/api/resource", json=payload, headers=headers)

Here, we not only set the request headers but also specify the JSON data to be sent using json=payload.

3. Sharing Request Headers Across Multiple Requests

If you find that certain request headers are the same for all requests (such as an authorization token), you might consider setting global request headers during user initialization to avoid redefining the same header information for each request:

from locust import HttpUser, task, between

class WebsiteUser(HttpUser):
    wait_time = between(1, 5)

    def on_start(self):
        """Called before each virtual user starts executing tasks"""
        self.headers = {
            'Authorization': 'Bearer your_access_token',
            'Content-Type': 'application/json'
        }

    @task
    def make_requests(self):
        self.client.get("/api/resource1", headers=self.headers)
        self.client.post("/api/resource2", json={"key": "value"}, headers=self.headers)

By setting the global variable self.headers in the on_start method, we can reference this variable anywhere without needing to redefine the same request headers each time.

How to Configure HTTP Request Headers in Locust

Notes

Security: Do not hard-code sensitive information (such as API keys, passwords, etc.) directly in the code. It is recommended to use environment variables or external configuration files to manage this information.

Dynamic Updates: If certain values in the request headers are dynamically changing (for example, based on the results of the previous request), ensure that these values are correctly updated before each request.

Error Handling: In practical applications, appropriate error handling logic should be added to deal with network exceptions or server error responses.

How to Configure HTTP Request Headers in Locust

How to Configure HTTP Request Headers in Locust

Every interaction is encouragement,

Every support promotes growth.

Leave a Comment