Open In App

How to Store Cookie Value Using Postman Pre-request Script?

Cookies are an important aspect of API development and are used to implement features like authentication, storing user data, session data, etc. This article focuses on discussing how to use Postman pre-request scripts for storing and manipulating cookies in Postman to test all features related to cookies in REST APIs. We will also use a NodeJS server to verify that the cookies were added successfully.

What are HTTP cookies?

HTTP Cookies are blocks of data that are stored on the client’s browser. Cookies can be created both by the server and the client. You can go to the developer tools and inspect cookies there (in the Application tab) and also add your own cookies. Whenever an HTTP request is sent the cookies in the browser are sent along with it to the server. The HTTP response sent by the server often contains a ‘Set-cookie’ header which represents the new cookies that are to be added to the client’s browser. The browser automatically values and adds the cookies themselves.



What are Pre-request Scripts in Postman?

Pre-request scripts in Postman are scripts that execute before the request is sent. It is mostly used for preprocessing data before sending it with the request. It can be accessed using the Pre-request tab in Postman. Apart from this pre-request can also be used for debugging the code. Usage of pre-request could be as follows:

Why are Cookies Required in API testing?

Some APIs can have features which cannot work without the use of cookies. So, it is essential to have cookies while testing such features. Luckily Postman has the ability to store cookies and send them with HTTP requests similar to as it is done in browsers. In Postman, cookies are mostly used to test authentication through tokens.



There are two types of scripts in Postman:

  1. Test scripts: They are executed after a response is received from the server. They are used to test the status code and the response received.
  2. Pre-request scripts: They are executed before a response is sent to the server. They are used to do some setup required before sending the request like setting some headers, building the request body, setting cookies and debugging.

For the purpose of storing cookies to send them with our request we will be using Postman pre-request scripts in this article.

Using Postman Pre-request Scripts

To demonstrate how to store cookie values using Postman’s pre-request scripts, we’ll go through a step-by-step example. But before writing the pre-request script we need to build a NodeJS server to test if cookies were added to the request or not:

1. Create a directory named ‘postman-cookies’.

2. Inside a directory execute the following command.

npm install express

3. Add a file app.js to the directory and paste the following code in it. This is a simple express server that’s prints the headers sent with the request and replies with a 200-status code.




const express = require("express");
const app = express();
 
app.use(express.json());
 
app.get("/home", (req, res, next) => {
    console.log(req.headers);
    res.status(200).json({ success: true });
});
 
app.listen(3000, () => {
    console.log("Listening on port 3000");
});

4. Start the server with the command:

node app.js

5. Launch Postman and create a request in it.

6. Enter http://localhost:3000 in the URL field and set the HTTP method to GET.

7. Now go to Pre-request Script tab. In the pre-request script, the request object can be accessed using pm.request object. Paste the following script in the pre-request tab. This script just adds a Cookie header in the request. (The cookies are stored inside the cookie header in an HTTP request).

8. Now you are all set. Hit the send button which triggers the request to server.

9. In the terminal you will get an output similar to this. The output contains a cookie field which was set by Postman pre-request script which we wrote.

There are alternate ways to add cookies in Postman apart from pre-request scripts:

  1. Using Cookie header in the Headers tab.
  2. Using the Cookies option under the send button.

Conclusion

Although cookies can be added using the graphical user interface provided by Postman, using pre-request scripts for the same helps to write automated tests which makes the API testing stage much faster, efficient and accurate. You can test your APIs over many test cases with just one click using pre-request scripts. You can also use the collection runner to run integrated tests that involves multiple related APIs that form a pipeline in which each request is dependent on a previous one. For example, a feature like resetting password, where multiple APIs are used such as for sending new password, sending OTP and login. There are many possibilities with pre-request scripts in Postman.


Article Tags :