Load Testing JSON‑API Chaining

Guide on chaining dependent JSON API calls using LoadForge and Locust.

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

Complex workflows often require multiple API calls: fetch a list of resources, pick random IDs, then retrieve or modify them. This guide shows you how to parse JSON responses and chain HTTP calls in a single Locust script.

Locust Test Script (locust.py)

# locust.py
import random
from locust import HttpUser, between, task

class JSONAPIUser(HttpUser):
    wait_time = between(1, 3)
    host = "https://api.example.com"

    def fetch_resources(self):
        response = self.client.get("/api/items", name="Get Items List")
        self.item_ids = [item['id'] for item in response.json()]

    def on_start(self):
        # Warm up: fetch initial resource list
        self.fetch_resources()

    @task(3)
    def read_item(self):
        # Randomly pick an existing item
        item_id = random.choice(self.item_ids)
        self.client.get(f"/api/items/{item_id}", name="Get Item Detail")

    @task(1)
    def create_and_delete(self):
        # Create a new item
        response = self.client.post(
            "/api/items",
            name="Create Item",
            json={"name": "LoadForge Item"}
        )
        new_id = response.json().get("id")
        if new_id:
            # Clean up by deleting it
            self.client.delete(
                f"/api/items/{new_id}",
                name="Delete Item"
            )
            # Refresh list occasionally
            self.fetch_resources()

Notes:

  • Install dependencies: pip install locust.
  • Adjust endpoint paths/names to suit your API schema.
  • Use self.client.headers.update(...) for auth headers if needed.

Ready to run your test?
Launch your locust test at scale.