- Postman - Discussion
- Postman - Useful Resources
- Postman - Quick Guide
- Postman - OAuth 2.0 Authorization
- Postman - Run Collections using Newman
- Postman - Newman Overview
- Postman - Sessions
- Postman - Cookies
- Postman - Mock Server
- Postman - Assertion
- Postman - Collection Runner
- Postman - Parameterize Requests
- Postman - Create Collections
- Postman - Create Tests for CRUD
- Postman - DELETE Requests
- Postman - PUT Requests
- Postman - POST Requests
- Postman - GET Requests
- Postman - Workflows
- Postman - Authorization
- Postman - Environment Variables
- Postman - Environment Setup
- Postman - Introduction
- Postman - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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](/postman/images/cookies1.jpg)
In the Cookies tab, the same cookie details will also get displayed.
![Cookies2](/postman/images/cookies2.jpg)
Cookies Management
In Postman, we can manage cookies by addition, deletion, and modification of cookies. Under the Params tab, we have the Cookies pnk to perform operations on cookies.
![Cookies3](/postman/images/cookies3.jpg)
Cpck on the Cookies pnk. MANAGE COOKIES pop-up shall open where all the available cookies are present with the option to add and delete a cookie.
![Delete Cookies](/postman/images/delete_cookie.jpg)
Cookies Addition
Follow the steps given below for adding cookie in Postman −
Step 1 − Cpck on the Add Cookie button. A text box shall open up with pre-existing values inside it. We can modify its values and then cpck on Save.
![Cookies Addition](/postman/images/cookies_addition.jpg)
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](/postman/images/cookie.jpg)
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