I\'m using Postman to test and play with an API.
For the login url, the API requires sending a POST request with username
and password
as fiel
Store the cookie value you want to use in a global variable.In Tests
tab of login request, write
postman.setGlobalVariable('key', postman.getResponseCookie("cookieName").value);
Pass along with the value in the Headers
tab as a cookie in get user request:
Cookie | cookieName={{key}}
I tried using Ashutosh's answer but got an error. I'm guessing this is because Postman's scripting API changed?
At any rate, the following worked for me:
Tests
tab of the request that will return cookies you want to save, writepm.globals.set('<your key>', pm.cookies.get('<cookie name>'));
cookie
and corresponding value as <your cookie name>={{<global variable name>}};
.I found documentation for this at the Postman sandbox API reference.
It seems there are two Interceptor plugin in google chrome. make sure install the correct one.
(Updated Dec 18 2020, using the native Postman app without the interceptor)
The traditional way of reading the cookie does not work for me pm.cookies.get('<cookie name>')
. Here is a workaround that automatically attaches auth cookie to all requests within a collection:
// The test scripts below run after the api /login returns the response
const authCookie = pm.response.headers.idx(3).value
/*
pm.response.headers.idx(3) is equal to:
{key: "Set-Cookie", value: "xs=eyJhb; Max-Age=3600; Path=/; Expires=Fri, 18 Dec 2020 04:40:34 GMT; HttpOnly; Secure; SameSite=None"}
*/
const token = authCookie.substring(3, authCookie.indexOf(';'))
pm.collectionVariables.set('xs_value', token);
Then add this pre-request scripts to the entire collection:
// Scripts below runs before any request within a collection is sent
const token = pm.collectionVariables.get('xs_value')
pm.request.headers.upsert({ key: 'Cookie', value: `xs=${token}` })
Enjoy!
More info on how to attach headers to requests