Postman - Cookies



The cookies are information sent by the server and stored in the browser. As soon as a request is sent, the cookies are returned by the server. In Postman, the cookies are mentioned under the Headers and Cookies tab in the Response.

Let us apply a GET request on an endpoint and find the cookies.

In the Headers tab, the cookie sent by the server is set with the key − set−cookie.

Cookies1

In the Cookies tab, the same cookie details will also get displayed.

Cookies2

Cookies Management

In Postman, we can manage cookies by addition, deletion, and modification of cookies. Under the Params tab, we have the Cookies link to perform operations on cookies.

Cookies3

Click on the Cookies link. MANAGE COOKIES pop-up shall open where all the available cookies are present with the option to add and delete a cookie.

Delete Cookies

Cookies Addition

Follow the steps given below for adding cookie in Postman −

Step 1 − Click on the Add Cookie button. A text box shall open up with pre-existing values inside it. We can modify its values and then click on Save.

Cookies Addition

Step 2 − Send the request again to the server.

The Response code obtained is 200 OK. Also, the Cookies tab in the Response now shows the newly added cookie − Cookie_Postman.

Cookie

Access Cookies via Program

Cookies can be handled programmatically without using the GUI in Postman. To work with cookies, we have to first generate a Cookie jar. It is an object which has all the cookies and methods to access them.

Cookie Jar Creation

The syntax for Cookie Jar Creation is as follows −

const c = pm.cookies.jar();

Cookie Creation

We can create a cookie with the .set() function. It accepts URL, name of cookie, value of cookie as parameters.

The syntax for cookie creation is as follows −

const c = pm.cookies.jar();
c.set(URL, name of cookie, value of cookie, callback(error, cookie));

Get Cookie

We can get a cookie with the .get() function. It accepts the URL, name of cookie as parameters. It yields the cookie value.

The syntax for getting a cookie is as follows −

const c = pm.cookies.jar();
c.set(URL, name of cookie, value of cookie, callback(error, cookie));
c.get(URL, name of cookie, callback(error, cookie));

Get All Cookies

We can get all cookies of a specific URL within a Cookie jar with the .getAll() function. It accepts a URL as a parameter. It yields all the cookie values for that URL.

The syntax for getting all cookies is as follows −

const c = pm.cookies.jar();
c.set(URL, name of first cookie, value of first cookie, callback(error, cookie));
c.set(URL, name of second cookie, value of second cookie, callback(error, cookie));
c.getAll(URL, callback(error, cookie));

Delete Cookie

We can delete a cookie with the .unset() function. It accepts the URL, name of cookie to be deleted as parameters.

The syntax for deleting cookie is as follows −

const c = pm.cookies.jar();
c.set(URL, name of cookie, value of cookie, callback(error, cookie));
c.unset(URL, name of cookie, callback(error, cookie));

Delete All Cookies

We can delete all cookies of a specific URL with the .clear() function. It accepts a URL as a parameter. It removes all the cookie values for that URL.

The syntax for deleting all cookies is as follows −

const c = pm.cookies.jar();
c.set(URL, name of first cookie, value of first cookie, callback(error, cookie));
c.set(URL, name of second cookie, value of second cookie, callback(error, cookie));
c.clear(URL, callback(error, cookie));
Advertisements