Cookies

Cookies are any information that is saved by websites for later use. Cookies are stored as textual data and can be used for different purposes, such as updating the user, showing the preferences for the as per the earlier searches and logins.

For example, in many areas, we have seen several times, that when we visit the same webpage which we visited earlier, we get to see some suggestions and recommendations of some pages or items which we searched earlier. This is where cookies come into play.

Let us consider an example of, amazon.in, when we make several searches for some items. And next time when we visit the same website again, we get to see many related items which are related to our searches. If there is any update for our searched items, then they are displayed into our dashboard or on the page we log in.

Why Cookies are required?

When a request is made to a server, the server considers it as a brand new request. Now, if the same request is sent over the server, again and again, the server considers it as a new request only. Now when a request is sent for the first time into the server, the server sends the response to the client along with the cookie.

Now, the response from the server may be used by you or may be discarded by the cookie along with the response id is saved into your local machine. Now, when you send the same request to the server, from your machine, the request along with the cookie is sent into the server, and the server identifies you as the earlier user.

Now, when the server knows it is you, then it can easily show or display all the preferences and updates which are relevant to you.

Changes in cookies

From time to time, cookies have gained its popularity in the application in a variety of fields. Cookies can help in different ways such as saving passwords, saving the language we want to use on a website, updating about different items we make searches of, etc.

Now, the problem with cookie is that, when we send a request and get the response back from the server, the server also sends cookies data along with the response to be saved in our local machine. Now, when this happens every time, the cookies, are stored in your local system, again and again. This creates a load into the system which is not good.

Thus later, engineers have found out a way where the cookie data is not directly stored in the local system but is stored in the related company database itself. In such a case, only the id for the specific cookie will be stored on your local computer. In this way, all the cookie data is stored into the server, thus reducing the load in the local system.

Why Cookies may not be good always

Cookies may not be good every time. Cookies are saved into a related server and only the website which is run into the same server can access the cookie. Or you can say like, your cookies cannot be used by any other server as it is directly mapped to related website's database or dataserver.

Also, a cookie is made to use only that specific data related to your website, which is required to provide you the recommendations, updates, reminders and some frequently saved attributes such as passwords, website languages, etc.

A cookie cannot be used by any third party but can be used of course with valid permission from the server provider. But this done sometime, only to set preferences and updates for your mutual benefit.

Different Types of Cookies

Cookies can be broadly divided into two types:

  • First-Party Cookies
  • Third-Party Cookies
First-Party Cookies:

First party cookies are the cookies are used by only your website to set preferences, updates that are relevant for your personal use of the website. For example, when we visit a site, your location is automatically detected and while making a search for a destination, it will automatically show you the route from your current destination.

Another example maybe when you log into your website, the weather in the current location will be automatically displayed by your website.

Third-party Cookies

Third-party cookies are the cookies that are used for advertisements, recommendations, etc. These cookies are used by different companies and organizations to detect user behavior and requirements. And based on the behavior and requirements, they run certain ad-campaigns and recommend some of the products which they think may be suitable for you.

Cookies In POSTMAN

Cookies application and uses can be checked inside your native web browser also. But we shall see the same using POSTMAN. To use cookies in POSTMAN, just click on the cookies link on the interface.

postman-cookie-option

Click on the cookie option and you will be able to see the below interface.

postman-cookie-edit

Programmatic access of cookies

Programmatic access to cookies means, to create and manipulate cookies with the help of program codes instead of relying on the graphical interface. This is possible in POSTMAN, where manipulating a cookie can be carried out with the help of some methods.

To perform any kind of operation on Cookies, first, you have to create a Cookie Jar. It is an object which contains the cookies and all the methods which will be used to perform any operation on cookies.

Creating a Cookies Jar

To create a cookie jar, just you have to use the function pm.cookies.jar() method. It will create an object containing the cookies and the methods for cookies manipulation.

Now, store the cookie object attributes into some variable(which is constant).

const cookieJar = pm.cookies.jar();Now the first step is completed. Now the next step is to create the cookie. Here, cookieJar is a variable that is declared as constant and is holding the object instance of cookies.

The next step is to set the cookie using .set() function. This function takes in the URL, cookie name, cookie name, and cookie value and a callback have to be set.

So to set the cookie, we have to use the following syntax:

const cookieJar = pm.cookies.jar();  //    creating a cookie jar.
cookieJar.set(URL, cookie name, callback(error, cookie));   // this is to create the cookie

The function .get() can be used to get the cookie value. This function takes the URL and the cookie name to create a cookie.

The syntax is:

cookieJar.get(URL, cookie name, callback(error, cookie));

in this syntax cookieJar is the cookie variable.

Get all the cookies

To get all the cookies for a unique URL, that is contained in the cookie jar the function .getAll() can be used. This function takes the URL as the parameter and returns all the cookies for the URL.

To delete a cookie, the function .unset() can be used. This function takes the URL and the cookies to be removed.

Syntax:

cookieJar.unset(URL, cookie name, callback(error));

Here, cookieJar is any cookie object created.

Delete all the cookies

All the cookies can be cleared using the function .clear(). This function takes only the URL for which all the cookies have to be removed.

This function will remove all the cookies for the related URL in the Jar, but not all the cookies in the jar as there can also be cookies from different URLs also.
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions