Cookies and sessions in API testing
Plenty of APIs don't use tokens at all. They log you in with a form or a JSON POST, answer with Set-Cookie: sid=…, and expect that cookie on every request after. That's how most web app backends, admin panels and older APIs work. Testing them is easy, as long as your API client handles cookies like a browser.
It just works in a collection run
When a response sets a cookie, Requestarium keeps it and sends it with the later requests that match it. So this collection needs no scripts:
POST {{baseUrl}}/loginwith the credentials. The response setssid.GET {{baseUrl}}/account. The cookie is sent automatically.POST {{baseUrl}}/logout.
Every request in a run-collection shares one cookie jar.
requestarium run-collection "Admin API" --env StagingBrowser rules, not shortcuts
Cookies follow the browser rules (RFC 6265):
- Domain and path. A cookie for
api.example.comisn't sent toauth.example.com, unless it was set for.example.com. A cookie withPath=/adminonly goes to/admin/…. Securecookies are sent only over HTTPS (and tolocalhost).Max-AgeandExpiresare respected.Max-Age=0deletes the cookie.__Host-and__Secure-prefixes are enforced.- Redirects. A login that answers
302withSet-Cookiekeeps the cookie for the next hop. TheAuthorizationheader is not passed on to another origin.
That matters for tests: if your API sets a cookie with the wrong domain or path, the test fails the same way a browser would.
Continue a session across commands
By default the jar lasts for one command. To log in once and run several commands on the same session, save the jar to a file:
requestarium run "Login" --env Staging --cookie-jar cookies.json
requestarium run-collection "Admin API" --folder Reports --env Staging --cookie-jar cookies.jsonrequestarium cookies --cookie-jar cookies.json # domain, path, name, expiry; values masked
requestarium cookies clear --cookie-jar cookies.jsonRead and set cookies in scripts
The Postman-style cookie API works:
pm.test('login sets a session cookie', () => {
pm.expect(pm.response.cookies.get('sid')).to.be.a('string');
});
pm.cookies.get('sid'); // a cookie sent to this request's URL
pm.cookies.has('sid');
pm.cookies.toObject(); // { sid: '…', theme: 'dark' }
const jar = pm.cookies.jar();
jar.set('https://api.example.com', 'lang', 'de');
jar.unset('https://api.example.com', 'lang');
jar.clear('https://api.example.com');The Thunder Client style (pm.getCookies, pm.setCookie, pm.clearCookies) works too.
Test the security attributes
Cookies are a common place for security bugs. A post-response test on your login request can check the attributes are set:
const setCookie = pm.response.headers.get('Set-Cookie') || '';
pm.test('session cookie is HttpOnly, Secure and SameSite', () => {
pm.expect(setCookie).to.include('HttpOnly');
pm.expect(setCookie).to.include('Secure');
pm.expect(setCookie).to.match(/SameSite=(Lax|Strict)/i);
});Your own Cookie header still works
If a request sets a Cookie header itself, it's sent too. When it and the jar have a cookie with the same name, the request's value wins. To test without any cookies, add --no-cookies.
Values are masked
Cookie and Set-Cookie values are masked as •••••• in reports, the JUnit file and --verbose output, so a CI log never leaks a live session.