13
votes

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?

3

3 Answers

28
votes

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

enter image description here

0
votes

You should look at this article.

0
votes

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.