Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set cookie in Postman with variable

Tags:

postman

I'm trying to write some postman tests that require me to set a cookie. My first request returns a token in a json payload. I've pulled this out and stored it in a variable but I cant seem to inject this into a cookie for further requests.

I get the value for the cookie like:

var json = pm.response.json();
pm.globals.set("my-cookie", json.Token);

I've tried this in the cookie dialog box:

my-cookie={{my-cookie}}; path=/; domain=.myhost.com; Expires=Tue, 19 Jan 2038 03:14:07 GMT;

However when it sends the cookie it sends the above without replacing the variable the, i.e. it has {{my-cookie}} rather than the value of the variable.

I cant see anything in the postman API that will enable me to set cookies on a request grammatically.

I did see this, but it only mentions adding them via the dialog

https://www.getpostman.com/docs/v6/postman/sending_api_requests/interceptor_extension

I also found this, but could find no mention of how to use it.

https://www.postmanlabs.com/postman-collection/Cookie.html

I'm assuming this is a common requirement and must be missing something obvious. Does anyone know how to do this?

like image 492
vickirk Avatar asked Jul 03 '18 13:07

vickirk


People also ask

Where are Postman cookies stored?

Postman stores cookies inside a Cookie Manager and the stored cookies can be accessed when writing scripts in Postman. The access to cookies inside scripts is exposed thanks to a cookie jar, and you can use that cookie jar to work with cookies inside your scripts. Select a request > Cookies.


Video Answer


2 Answers

Try using the Headers feature with a key of Cookie and a value of cookieName={{yourVariable}}

enter image description here

like image 126
Josh Avatar answered Oct 19 '22 06:10

Josh


Postman - how to set cookies in request

In case anyone stumbles upon this question, in current version there is a special section for setting request cookies on the righthand side of the request window.

like image 26
zeldi Avatar answered Oct 19 '22 05:10

zeldi