Header and Cookie Managers
Two things travel with an HTTP request and are not in its path: the headers and the cookies. A plan sets headers in an HTTP Header Manager and lets an HTTP Cookie Manager keep the cookies the server sets. This lesson runs the login of the course server, carries its token in a header and its cookie in the jar, and shows the assertion that catches the request where the header is missing.
The plan
The cookie manager is a child of the thread group, so every request below it shares one jar. The header managers are children of the two samplers that need a token, so the login and the two echo requests do not send one: a manager under a sampler applies to that sampler only.
plan.jmx
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.6.3">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="A token and a cookie" enabled="true">
<boolProp name="TestPlan.functional_mode">false</boolProp>
<boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
<elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments">
<elementProp name="host" elementType="Argument">
<stringProp name="Argument.name">host</stringProp>
<stringProp name="Argument.value">127.0.0.1</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="port" elementType="Argument">
<stringProp name="Argument.name">port</stringProp>
<stringProp name="Argument.value">8099</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
</collectionProp>
</elementProp>
</TestPlan>
<hashTree>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<stringProp name="LoopController.loops">1</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">1</stringProp>
<stringProp name="ThreadGroup.ramp_time">1</stringProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<stringProp name="ThreadGroup.duration"/>
<stringProp name="ThreadGroup.delay"/>
</ThreadGroup>
<hashTree>
<ConfigTestElement guiclass="HttpDefaultsGui" testclass="ConfigTestElement" testname="HTTP Request Defaults" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="HTTPSampler.domain">${host}</stringProp>
<stringProp name="HTTPSampler.port">${port}</stringProp>
<stringProp name="HTTPSampler.protocol">http</stringProp>
<stringProp name="HTTPSampler.contentEncoding"/>
<stringProp name="HTTPSampler.path"/>
</ConfigTestElement>
<hashTree/>
<CookieManager guiclass="CookiePanel" testclass="CookieManager" testname="HTTP Cookie Manager" enabled="true">
<collectionProp name="CookieManager.cookies"/>
<boolProp name="CookieManager.clearEachIteration">true</boolProp>
<boolProp name="CookieManager.controlledByThreadGroup">false</boolProp>
</CookieManager>
<hashTree/>
<ResultCollector guiclass="SimpleDataWriter" testclass="ResultCollector" testname="results.jtl" enabled="true">
<boolProp name="ResultCollector.error_logging">false</boolProp>
<objProp>
<name>saveConfig</name>
<value class="SampleSaveConfiguration">
<time>true</time>
<latency>true</latency>
<timestamp>true</timestamp>
<success>true</success>
<label>true</label>
<code>true</code>
<message>true</message>
<threadName>true</threadName>
<dataType>true</dataType>
<encoding>false</encoding>
<assertions>true</assertions>
<subresults>false</subresults>
<responseData>false</responseData>
<samplerData>false</samplerData>
<xml>false</xml>
<fieldNames>true</fieldNames>
<responseHeaders>false</responseHeaders>
<requestHeaders>false</requestHeaders>
<responseDataOnError>false</responseDataOnError>
<saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
<assertionsResultsToSave>0</assertionsResultsToSave>
<bytes>true</bytes>
<sentBytes>true</sentBytes>
<url>true</url>
<threadCounts>true</threadCounts>
<idleTime>true</idleTime>
<connectTime>true</connectTime>
</value>
</objProp>
<stringProp name="filename">results.jtl</stringProp>
</ResultCollector>
<hashTree/>
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="secure without a token" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="HTTPSampler.domain">${host}</stringProp>
<stringProp name="HTTPSampler.port">${port}</stringProp>
<stringProp name="HTTPSampler.protocol">http</stringProp>
<stringProp name="HTTPSampler.contentEncoding"/>
<stringProp name="HTTPSampler.path">/secure</stringProp>
<stringProp name="HTTPSampler.method">GET</stringProp>
<boolProp name="HTTPSampler.follow_redirects">true</boolProp>
<boolProp name="HTTPSampler.auto_redirects">false</boolProp>
<boolProp name="HTTPSampler.use_keepalive">true</boolProp>
<boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
<stringProp name="HTTPSampler.connect_timeout"/>
<stringProp name="HTTPSampler.response_timeout"/>
</HTTPSamplerProxy>
<hashTree>
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="a 401" enabled="true">
<collectionProp name="Asserion.test_strings">
<stringProp name="5843">401</stringProp>
</collectionProp>
<stringProp name="Assertion.custom_message"/>
<stringProp name="Assertion.test_field">Assertion.response_code</stringProp>
<boolProp name="Assertion.assume_success">true</boolProp>
<intProp name="Assertion.test_type">8</intProp>
</ResponseAssertion>
<hashTree/>
</hashTree>
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="login" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="HTTPSampler.domain">${host}</stringProp>
<stringProp name="HTTPSampler.port">${port}</stringProp>
<stringProp name="HTTPSampler.protocol">http</stringProp>
<stringProp name="HTTPSampler.contentEncoding"/>
<stringProp name="HTTPSampler.path">/login</stringProp>
<stringProp name="HTTPSampler.method">GET</stringProp>
<boolProp name="HTTPSampler.follow_redirects">true</boolProp>
<boolProp name="HTTPSampler.auto_redirects">false</boolProp>
<boolProp name="HTTPSampler.use_keepalive">true</boolProp>
<boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
<stringProp name="HTTPSampler.connect_timeout"/>
<stringProp name="HTTPSampler.response_timeout"/>
</HTTPSamplerProxy>
<hashTree/>
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="echo with the cookie and the token" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="HTTPSampler.domain">${host}</stringProp>
<stringProp name="HTTPSampler.port">${port}</stringProp>
<stringProp name="HTTPSampler.protocol">http</stringProp>
<stringProp name="HTTPSampler.contentEncoding"/>
<stringProp name="HTTPSampler.path">/echo</stringProp>
<stringProp name="HTTPSampler.method">GET</stringProp>
<boolProp name="HTTPSampler.follow_redirects">true</boolProp>
<boolProp name="HTTPSampler.auto_redirects">false</boolProp>
<boolProp name="HTTPSampler.use_keepalive">true</boolProp>
<boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
<stringProp name="HTTPSampler.connect_timeout"/>
<stringProp name="HTTPSampler.response_timeout"/>
</HTTPSamplerProxy>
<hashTree>
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="the cookie went" enabled="true">
<collectionProp name="Asserion.test_strings">
<stringProp name="40471">session=abc123</stringProp>
</collectionProp>
<stringProp name="Assertion.custom_message"/>
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
<boolProp name="Assertion.assume_success">false</boolProp>
<intProp name="Assertion.test_type">2</intProp>
</ResponseAssertion>
<hashTree/>
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="the token went" enabled="true">
<collectionProp name="Asserion.test_strings">
<stringProp name="41518">Bearer secret-token</stringProp>
</collectionProp>
<stringProp name="Assertion.custom_message"/>
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
<boolProp name="Assertion.assume_success">false</boolProp>
<intProp name="Assertion.test_type">2</intProp>
</ResponseAssertion>
<hashTree/>
<HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP Header Manager" enabled="true">
<collectionProp name="HeaderManager.headers">
<elementProp name="" elementType="Header">
<stringProp name="Header.name">Authorization</stringProp>
<stringProp name="Header.value">Bearer secret-token</stringProp>
</elementProp>
</collectionProp>
</HeaderManager>
<hashTree/>
</hashTree>
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="echo with the cookie only" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="HTTPSampler.domain">${host}</stringProp>
<stringProp name="HTTPSampler.port">${port}</stringProp>
<stringProp name="HTTPSampler.protocol">http</stringProp>
<stringProp name="HTTPSampler.contentEncoding"/>
<stringProp name="HTTPSampler.path">/echo</stringProp>
<stringProp name="HTTPSampler.method">GET</stringProp>
<boolProp name="HTTPSampler.follow_redirects">true</boolProp>
<boolProp name="HTTPSampler.auto_redirects">false</boolProp>
<boolProp name="HTTPSampler.use_keepalive">true</boolProp>
<boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
<stringProp name="HTTPSampler.connect_timeout"/>
<stringProp name="HTTPSampler.response_timeout"/>
</HTTPSamplerProxy>
<hashTree>
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="the token went" enabled="true">
<collectionProp name="Asserion.test_strings">
<stringProp name="41518">Bearer secret-token</stringProp>
</collectionProp>
<stringProp name="Assertion.custom_message"/>
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
<boolProp name="Assertion.assume_success">false</boolProp>
<intProp name="Assertion.test_type">2</intProp>
</ResponseAssertion>
<hashTree/>
</hashTree>
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="secure with the token" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="HTTPSampler.domain">${host}</stringProp>
<stringProp name="HTTPSampler.port">${port}</stringProp>
<stringProp name="HTTPSampler.protocol">http</stringProp>
<stringProp name="HTTPSampler.contentEncoding"/>
<stringProp name="HTTPSampler.path">/secure</stringProp>
<stringProp name="HTTPSampler.method">GET</stringProp>
<boolProp name="HTTPSampler.follow_redirects">true</boolProp>
<boolProp name="HTTPSampler.auto_redirects">false</boolProp>
<boolProp name="HTTPSampler.use_keepalive">true</boolProp>
<boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
<stringProp name="HTTPSampler.connect_timeout"/>
<stringProp name="HTTPSampler.response_timeout"/>
</HTTPSamplerProxy>
<hashTree>
<HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP Header Manager" enabled="true">
<collectionProp name="HeaderManager.headers">
<elementProp name="" elementType="Header">
<stringProp name="Header.name">Authorization</stringProp>
<stringProp name="Header.value">Bearer secret-token</stringProp>
</elementProp>
</collectionProp>
</HeaderManager>
<hashTree/>
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="a 200" enabled="true">
<collectionProp name="Asserion.test_strings">
<stringProp name="72676">200</stringProp>
</collectionProp>
<stringProp name="Assertion.custom_message"/>
<stringProp name="Assertion.test_field">Assertion.response_code</stringProp>
<boolProp name="Assertion.assume_success">false</boolProp>
<intProp name="Assertion.test_type">8</intProp>
</ResponseAssertion>
<hashTree/>
</hashTree>
</hashTree>
</hashTree>
</hashTree>
</jmeterTestPlan>
The plan talks to the course server of the sixth lesson; its whole file is repeated here, so that this page runs on its own.
server.py
#!/usr/bin/env python3 """The target server of the JMeter course: a small JSON API on 127.0.0.1. python3 server.py 8099 Every endpoint is a few lines: the course tests them, it does not test the server. /slow sleeps, /fail answers 500, /login sets a cookie and returns a token, /secure wants that token in an Authorization header. """ import json import sys import time import uuid from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from urllib.parse import parse_qs, urlparse USERS = [{"id": 1, "name": "Ada"}, {"id": 2, "name": "Grace"}, {"id": 3, "name": "Linus"}] TOKEN = "secret-token" class Handler(BaseHTTPRequestHandler): """One request, one small JSON answer.""" protocol_version = "HTTP/1.1" def log_message(self, fmt, *args): """Keep the course\'s server log quiet: every request would be a line.""" def reply(self, status, body, ctype="application/json", headers=None): """Send a body with its length, so a keep-alive client can read it.""" data = body.encode("utf-8") self.send_response(status) self.send_header("Content-Type", ctype) self.send_header("Content-Length", str(len(data))) for name, value in (headers or {}).items(): self.send_header(name, value) self.end_headers() self.wfile.write(data) def reply_json(self, status, value, headers=None): self.reply(status, json.dumps(value), headers=headers) def do_GET(self): url = urlparse(self.path) query = parse_qs(url.query) if url.path == "/": self.reply(200, "<!doctype html><title>course</title><h1>JMeter course</h1>", "text/html") elif url.path == "/health": self.reply_json(200, {"status": "ok"}) elif url.path == "/api/users": self.reply_json(200, {"users": USERS, "count": len(USERS)}) elif url.path.startswith("/api/users/"): user = next((u for u in USERS if str(u["id"]) == url.path.rsplit("/", 1)[-1]), None) self.reply_json(200 if user else 404, user or {"error": "no such user"}) elif url.path == "/items": page = int(query.get("page", ["1"])[0]) self.reply_json(200, {"page": page, "items": [f"item-{page}-{n}" for n in range(1, 4)]}) elif url.path == "/slow": ms = int(query.get("ms", ["300"])[0]) time.sleep(ms / 1000.0) self.reply_json(200, {"slept_ms": ms}) elif url.path == "/fail": self.reply_json(500, {"error": "the server was told to fail"}) elif url.path == "/login": self.reply_json(200, {"token": TOKEN}, headers={"Set-Cookie": "session=abc123; Path=/"}) elif url.path == "/secure": if self.headers.get("Authorization") == "Bearer " + TOKEN: self.reply_json(200, {"message": "welcome"}) else: self.reply_json(401, {"error": "a token is required"}) elif url.path == "/token": self.reply_json(200, {"token": uuid.uuid4().hex}) elif url.path == "/redirect": self.reply(302, "", headers={"Location": "/health"}) elif url.path == "/xml": self.reply(200, "<user><id>1</id><name>Ada</name></user>", "application/xml") elif url.path == "/echo": body = json.dumps({"method": "GET", "path": self.path, "headers": dict(self.headers)}) self.reply(200, body) else: self.reply_json(404, {"error": "not found", "path": url.path}) def do_POST(self): length = int(self.headers.get("Content-Length", "0")) raw = self.rfile.read(length).decode("utf-8") if length else "" if self.path == "/api/orders": try: order = json.loads(raw) if raw else {} except json.JSONDecodeError: self.reply_json(400, {"error": "the body is not JSON"}) return self.reply_json(201, {"id": 1001, "received": order}) else: self.reply_json(404, {"error": "not found", "path": self.path}) if __name__ == "__main__": port = int(sys.argv[1]) if len(sys.argv) > 1 else 8099 server = ThreadingHTTPServer(("127.0.0.1", port), Handler) print(f"listening on 127.0.0.1:{port}", flush=True) server.serve_forever()
python3 server.py 8099 &
jmeter -n -t plan.jmx -l results.jtl -j jmeter.log
Creating summariser <summary>
Created the tree successfully using plan.jmx
Starting standalone test @ 2026 Nov 24 21:55:40 GMT (1795557340346)
Waiting for possible Shutdown/StopTestNow/HeapDump/ThreadDump message on port 4445
summary = 5 in 00:00:01 = 6.2/s Avg: 47 Min: 2 Max: 90 Err: 1 (20.00%)
Tidying up ... @ 2026 Nov 24 21:55:41 GMT (1795557341918)
... end of run
What the five answered
The last two columns are the success flag and the assertion's message, and they are the ones that matter here: four samples passed and one failed. The fourth request, echo with the cookie only, asserts that the body contains the token, and the request has no header manager, so the server never saw a token and the assertion failed. The third request, with both, passed both assertions: the cookie arrived from the jar and the token from the header manager.
cut -d, -f3,4,5,8,9 results.jtl
label,responseCode,responseMessage,success,failureMessage
secure without a token,401,Unauthorized,true,
login,200,OK,true,
echo with the cookie and the token,200,OK,true,
echo with the cookie only,200,OK,false,Test failed: text expected to contain /Bearer secret-token/
secure with the token,200,OK,true,
A 401 is a failure
JMeter marks a sample as failed when the response code is 400 or above, before any assertion is asked. A plan that expects a 401 must say so twice: the assertion checks the code, and its Ignore Status flag (Assertion.assume_success in the file) stops JMeter from counting the 4xx as a failure on its own. The first sampler of this plan does both, which is why the summary counts one error and not two.
The cookie jar
/login answers with Set-Cookie: session=abc123. The cookie manager stores it and sends it on every later request of the same thread, which is why the two echo requests see it without a header manager. A plan that starts each user with a fresh session sets CookieManager.clearEachIteration, which this plan does: without it, a user of the second loop would come back with the cookie of the first.
The cookie is per thread, not per plan: two users have two jars, and a login by one does not give the other a session. That is what makes a load test realistic, and it is also the reason a plan that logs in once and then runs a thousand requests with the same cookie is measuring one user a thousand times.
The header manager
The header manager of the third and fifth samplers holds one pair: the name Authorization and the value Bearer secret-token. The value could be a variable, and in a real plan it is: the correlation lesson reads the token out of the login answer and puts it here. The course server keeps the token fixed, so this page can write it in the file and still prove the header arrived.
Related
- HTTP Request and Defaults: the requests these managers sit under.
- Correlation: the token taken out of the login answer instead of typed in.
- Assertions: the failure this page shows in the result file.
Article author: Arthur Isaev