Skip to content

JMeter Correlation and Dynamic Values

End-to-end JMeter correlation: extract CSRF tokens, session IDs, and JSON fields with Regex and JSON extractors, chain requests, and debug replays.

Difficulty
intermediate
Guide type
how-to
Estimated read time
12 min read
Last verified version
Verified JMeter 5.6

Correlation means capturing a value from one response and reusing it in later requests so a multi-step flow works under many threads. Without it, recorded scripts fail on replay: session IDs, CSRF tokens, order IDs, and JWTs change every run. This guide walks through extractors, variable scope, chaining patterns, debugging, and load-safe practice, grounded in JMeter regular expressions, functions and variables, best practices, and the component reference.

The HTTP(S) Test Script Recorder captures literal values from your session. On the next run the server issues new secrets. Typical dynamic fields:

  • Session cookies (often handled by HTTP Cookie Manager)
  • CSRF / anti-forgery tokens in HTML or JSON
  • OAuth state, nonce, code
  • JWT access_token
  • Resource IDs (orderId, cartId)
  • Pagination cursors

Cookies are correlated automatically if the Cookie Manager is present. Everything else needs post-processors (extractors) or explicit functions.

  1. Run the journey once with Tree view; note the first red sampler.
  2. Inspect the previous response body/headers for the missing value.
  3. Add an extractor under that previous sampler.
  4. Replace hard-coded text in later samplers with \${varName}.
  5. Set a Default Value (NOT_FOUND) on the extractor.
  6. Re-run one thread until green.
  7. Parameterize users with CSV; re-validate.
  8. Disable heavy listeners; run CLI load.
ToolBest for
JSON ExtractorREST JSON bodies (access_token, nested ids)
Regular Expression ExtractorHTML snippets, headers, mixed text
CSS Selector ExtractorHTML elements when CSS queries fit
XPath ExtractorXML / some HTML (costlier; use carefully under load)
Boundary ExtractorFixed left/right text boundaries
Cookie ManagerSet-Cookie / Cookie headers

For JSON APIs, prefer structured JSON extraction over fragile full-body regex when possible. For free text, regex remains standard; see the regular expressions chapter.

Documented concepts you will set on every regex extractor:

FieldRole
Name of created variablee.g. csrfToken\${csrfToken}
Regular ExpressionPattern with capture groups (...)
Template$1$ for first group, $0$ full match
Match No.1 first match; 0 random; negative for all + _matchNr
Default ValueUsed when no match (debug signal)

Example HTML:

<input type="hidden" name="_csrf" value="a1b2c3d4" />

Pattern (illustrative):

name="_csrf"\s+value="([^"]+)"

Template: $1$ → variable holds a1b2c3d4.

Build candidates faster with the Regex Extractor Builder (paste response locally in the browser; nothing is uploaded).

Login response:

{"access_token":"eyJ...","order":{"id":"ORD-9"}}
  1. JSON Extractor on login sampler: variable accessToken, path to token.
  2. Second extractor or multi-path config for orderId if supported by your element setup.
  3. Header Manager: Authorization: Bearer \${accessToken}.
  4. Next path: /orders/\${orderId}.

Always assert login success so empty tokens do not flood the next step.

From the functions manual:

  • Variables are thread-local. Thread 5 cannot read thread 4’s \${orderId} by default.
  • Properties are global to the JVM (\${__P} / __setProperty). Use for environment config, not per-user secrets under load.
  • Undefined \${name} is returned unchanged (no hard error). That is why defaults and assertions matter.

Extractors run as post-processors after their parent sampler (and according to scope rules in the tree). Put the extractor under the sampler that returns the value, not under a later sibling that never sees the response.

Login → extract token
Create cart → extract cartId
Add item → use cartId
Checkout → extract orderId
Get order → use orderId

Use Transaction Controllers to group steps for dashboard reporting (dashboard). Keep sampler labels stable (Login, CreateCart) so series and filters stay readable.

Controllers that interact with correlation

Section titled “Controllers that interact with correlation”
  • Once Only Controller: login/extract once per thread.
  • If Controller: branch when \${accessToken} equals default failure value.
  • While Controller: poll until status is ready (guard with max iterations to avoid infinite loops).
MechanismElement
Session cookieHTTP Cookie Manager
CSRF in HTML formRegex / CSS / Boundary extractor → form parameter
Bearer tokenHeader Manager + variable
Query parameter idPath or parameters field \${id}

Missing Cookie Manager is a top cause of “works in browser during record, fails in JMeter.”

  1. View Results Tree → Response data of the source sampler.
  2. Confirm the pattern matches in a single-thread run.
  3. Check variable with Debug Sampler or by seeing request values on the next sampler.
  4. Verify extractor scope (main sample vs sub-samples; redirects).
  5. Check character encoding and multiline flags for regex.
  6. Escape commas inside function parameters if you also use functions (functions guide).

Best practices: use as few assertions as needed under load; avoid heavy listeners. Same idea for extractors:

  • Prefer cheap JSON path over huge regex on multi-megabyte HTML.
  • Do not extract unused fields.
  • Do not log every variable on every sample.
  • Precompute CSV data when values are not server-dynamic.

In distributed mode, each worker is a separate JVM. Per-thread variables stay on that worker. CSV files used for users must exist on each worker (not auto-copied). Do not assume a property set on one engine is visible on another.

MistakeResult
Hard-coded recorded tokenImmediate or mid-test auth failures
Extractor on wrong samplerEmpty variable
No default valueSilent empty strings
Regex without group but template $1$Wrong or empty
Match No. wrongPicks stale or random match
Sharing tokens via propertiesCross-talk between users
Skipping Cookie ManagerSession lost
  1. Record or build a two-step flow.
  2. Correlate one token with regex or JSON.
  3. Add CSV users (best practices).
  4. Add JWT/OAuth header pattern if API-based.
  5. Run CLI + dashboard; confirm zero \${...} literals in failures.

Capturing dynamic values from responses (tokens, IDs) into variables and sending them on later requests so multi-step scenarios work for every thread.

JSON Extractor or Regular Expression Extractor?

Section titled “JSON Extractor or Regular Expression Extractor?”

For JSON responses, prefer JSON-oriented extractors. Use regular expressions for HTML fragments, headers, or unstructured text.

Why is my variable still showing as \${csrf}?

Section titled “Why is my variable still showing as \${csrf}?”

The variable was never set. JMeter leaves undefined references unchanged. Fix the extractor and use a default value to detect misses.

Section titled “Do I need extractors if I use a Cookie Manager?”

Cookies often work automatically with Cookie Manager. Body and header tokens still need extractors.

Can one thread read another thread’s extracted orderId?

Section titled “Can one thread read another thread’s extracted orderId?”

Not with ordinary variables. Variables are thread-local by design. Use properties only for intentional global data, not per-user IDs.

Replay with one thread, find the first failure, extract from the previous response, replace hard-coded values, and repeat until green.

On this page