Vegeta Attack Targets: Headers, Bodies, and Scenarios

Vegeta Attack Targets: Headers, Bodies, and Scenarios

A single echo "GET http://..." gets you started with Vegeta, but real applications don't have one endpoint. They have dozens. Users authenticate, then create records, then query those records. Load testing with a single GET misses most of what matters.

Vegeta handles multi-endpoint scenarios through its target file format. This post covers how to define targets -- including headers, request bodies, and authentication -- and how to build representative load scenarios.

The Target File Format

Vegeta reads targets from stdin or a file. Each target consists of a method and URL on the first line, optional headers, an optional body, and a blank line separator:

GET https://api.example.com/users
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9...
Accept: application/json

GET https://api.example.com/products
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9...
Accept: application/json

Run it:

vegeta attack -targets=targets.txt -rate=100 -duration=30s | vegeta report

Vegeta cycles through the targets in order, repeating from the top when it reaches the end. With two targets and 100 RPS, each endpoint gets roughly 50 RPS.

POST with a JSON Body

For POST requests, put the body inline or reference a file with @:

POST https://api.example.com/orders
Content-Type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9...
@order-body.json

The order-body.json file:

{
  "product_id": "prod_abc123",
  "quantity": 2,
  "shipping_address": {
    "city": "Berlin",
    "country": "DE"
  }
}

For small bodies, inline them directly:

POST https://api.example.com/search
Content-Type: application/json
{"q": "laptop", "page": 1, "limit": 20}

Note: inline bodies go on a single line directly after the headers, with no blank line between headers and body.

Authentication Headers

Most APIs require authentication. Add the token as a header in every target:

GET https://api.example.com/account/profile
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyXzEyMyJ9...
X-Request-ID: load-test-run-001

POST https://api.example.com/account/preferences
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyXzEyMyJ9...
Content-Type: application/json
{"theme": "dark", "notifications": true}

For API key authentication:

GET https://api.example.com/data
X-API-Key: sk_live_abc123xyz
Accept: application/json

Generating Targets Dynamically

Static target files can't vary parameters per request. For that, generate the file with a script before running the attack:

#!/bin/bash
TOKEN=$(curl -sf -X POST https://api.example.com/auth/token \
  -H "Content-Type: application/json" \
  -d '{"username":"loadtest","password":"secret"}' \
  | jq -r '.access_token')

> targets.txt  # clear the file

for USER_ID in $(seq 1 100); do
  echo "GET https://api.example.com/users/$USER_ID"
  echo "Authorization: Bearer $TOKEN"
  echo ""
done >> targets.txt

vegeta attack -targets=targets.txt -rate=200 -duration=60s | vegeta report

This tests 100 different user IDs, cycling through them at 200 RPS. It exercises your database cache more realistically than hammering a single record.

Multiple Endpoints with Different Weights

Vegeta cycles through targets evenly. If you want endpoint A to get 4x more traffic than endpoint B, repeat it 4 times in the file:

GET https://api.example.com/products
Authorization: Bearer $TOKEN

GET https://api.example.com/products
Authorization: Bearer $TOKEN

GET https://api.example.com/products
Authorization: Bearer $TOKEN

GET https://api.example.com/products
Authorization: Bearer $TOKEN

POST https://api.example.com/cart
Content-Type: application/json
Authorization: Bearer $TOKEN
{"product_id": "prod_abc", "quantity": 1}

With 5 targets total, the GET gets 80% of traffic and the POST gets 20%. This reflects real-world traffic patterns where reads outnumber writes.

Reading Targets from a File vs Stdin

Use -targets for a file:

vegeta attack -targets=targets.txt -rate=100 -duration=60s | vegeta report

Use stdin for generated or piped targets:

generate-targets.sh | vegeta attack -rate=100 -duration=60s | vegeta report

The generate-targets.sh script writes the target format to stdout. This is useful for CI pipelines where targets depend on environment variables or dynamic test data.

PUT and DELETE Requests

PUT https://api.example.com/users/usr_456/email
Content-Type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9...
{"email": "updated@example.com"}

DELETE https://api.example.com/sessions/sess_789
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9...

DELETE requests typically have no body. Omit the @file or inline body line and go straight to the blank separator.

Multipart Form Data

For file upload endpoints, reference a file that contains the raw multipart body:

POST https://api.example.com/documents/upload
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxk
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9...
@upload-body.bin

The upload-body.bin file contains the raw multipart-encoded payload. Generate it once with curl --trace-ascii or construct it manually for a fixed test file.

Scenario: Read-Heavy API

A read-heavy e-commerce API might look like this:

# 40% - product listings
GET https://api.example.com/products?category=electronics&page=1
Accept: application/json

GET https://api.example.com/products?category=electronics&page=2
Accept: application/json

GET https://api.example.com/products?category=clothing&page=1
Accept: application/json

GET https://api.example.com/products?category=clothing&page=2
Accept: application/json

# 40% - product detail pages
GET https://api.example.com/products/prod_001
Accept: application/json

GET https://api.example.com/products/prod_002
Accept: application/json

GET https://api.example.com/products/prod_003
Accept: application/json

GET https://api.example.com/products/prod_004
Accept: application/json

# 20% - cart operations
POST https://api.example.com/cart/items
Content-Type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9...
{"product_id": "prod_001", "quantity": 1}

POST https://api.example.com/cart/items
Content-Type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9...
{"product_id": "prod_002", "quantity": 2}

Run this at increasing rates to find where the write operations start degrading:

for RATE in 50 100 200 500 1000; do
  echo "=== ${RATE} RPS ==="
  vegeta attack -targets=ecommerce.txt -rate=$RATE -duration=30s > /tmp/result-$RATE.bin
  vegeta report /tmp/result-$RATE.bin
done

Checking What Vegeta Actually Sent

When debugging a target file, use -output with encode to inspect individual results:

vegeta attack -targets=targets.txt -rate=5 -duration=5s \
  | vegeta encode \
  | jq '{status: .status, url: .url, error: .error}'

This shows the status code, URL, and error for each request. If your target file has a formatting mistake -- missing blank line, bad URL -- you'll see 0 status codes with parse errors.

The target file format is the foundation of everything else in Vegeta. Once you have realistic targets, you can add metrics analysis, integrate with CI, or use the Go library for dynamic scenarios that the file format can't express.

Read more

Start now free