How to store and reuse cookies in Postman?

I use Postman to test and play with the API.

For the login URL, the API requires a POST request with username and password fields as fields. I do this, and I get a 200 response with a message that I am logged in.

Then I will try another query to get user data. However, I get the answer that I have not logged in.

I realized that this problem is most likely due to the fact that the cookie that is sent to me when I log in is not included in the next Postman request.

So my question is: how do I save and enable cookies for future requests?

+11
source share
4 answers

Store the cookie value you want to use in a global variable. On the Tests tab of the login request, enter

 postman.setGlobalVariable('key', postman.getResponseCookie("cookieName").value); 

Pass the cookie in the user request along with the value in the " Headers " tab:

 Cookie | cookieName={{key}} 
+8
source

There seem to be two Interceptor plugins in Google Chrome. make sure to install the correct one .

0
source

I tried using Ashutosh's answer, but got an error. I assume this is because the Postman scripting API has changed?

Anyway, the following worked for me:

  1. On the Tests tab of the request that returns the cookies you want to save, write
 pm.globals.set('<your key>', pm.cookies.get('<cookie name>')); 
  1. Then, as described in Ashutosh’s answer, add a cookie to the headers, setting the key as a cookie and the corresponding value as <your cookie name>={{<global variable name>}}; ,

I found the documentation for this in the Postman Sandbox API reference .

0
source

Source: https://habr.com/ru/post/1208023/


All Articles