Custom HTTP Headers

How to set custom headers with your requests

LoadForge can record your browser, graphically build tests, scan your site with a wizard and more. Sign up now to run your first test.

Sign up now


Overview

You can easily set any headers you need by tweaking your get() or post() calls to include a header set. This is commonly used for authentication on an API (see api examples)

The examples below show two methods of setting headers. You can set any key-value headers you need with LoadForge.

Code

Below we set our POST requests Content-Type to application/vnd.api+json and the GET request to application/json within the same test.

self.client.post(
    "/login",
    data=json.dumps(payload),
    headers={"Content-Type": "application/vnd.api+json"}
)

self.client.get("/api/test", headers={"Content-Type": "application/json"})

Global Headers

You can also set headers globally by defining the on_start() section as shown below. This will ensure ALL requests have the custom header added.

def on_start(self):
    self.client.headers.update({'Authorization': 'Bearer xxx'})
def on_start(self):
    self.client.headers.update({'Content-Type': 'application/json'})

Custom Host Header

Sometimes you need set a custom host header, for example if you are testing against an IP address or a non-standard port. You can do this by setting the Host header:

def on_start(self):
    self.client.headers.update({'Host': 'www.loadforge.com'})

Or, on a single get/post request:

self.client.get("/api/test", headers={"Host": "www.loadforge.com"})

These are snippets!

These are just partial examples from a test. You may use the wizard or another example template and copy paste these into your test.


Locust Test Example

LoadForge is powered by locust, meaning open source locust users can copy this script as well. If you are a locust user, consider importing your script to LoadForge to supercharge your testing!

Ready to run your test?
Start your first test within minutes.