Open In App

Pre-request and Post-request scripts in Postman

Last Updated : 07 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Postman is a powerful tool for testing APIs, and its pre-request and post-request scripts allow you to customize requests and responses. it is an API(utility programming interface) development device that enables construction, taking a look at and altering APIs. It could make numerous varieties of HTTP requests(GET, POST, PUT, PATCH), store environments for later use, and convert the API to code for various languages(like JavaScript, and Python). In this article, we will learn how to use pre-request scripts and post-request scripts in Postman.

In this Article, You’ll learn pre-request scripts in Postman, and post-request scripts in Postman.

Prerequisites:

Post-Request Scripts

In postman, we are able to write check scripts to check API requests in JavaScript.These are known as Post-request scripts. Test script is used to check whether or not your API is operating thus or now not, to set up that integrations among the offerings are functioning properly, and to test that new tendencies have not affected any functionality of current requests.

Steps to Execute Post-Request Scripts in Postman

Step 1: After downloading and installing the Postman, open the software. Add a new Collection and give it a name like “GFG”. Here, we can see, multiple tabs like Authorization, Pre-request scripts, Tests , Variables.

postman2Step 2: Click on Tests. Now in the right side pane, we can multiple snippets arrived. We can use any of the available snippet or create our own code based on our requirement. Code is written in Javascript.

z165

We will write 2 snippets in JavaScript from the already available snippets from the right-hand side pane.

Script 1: This will check whether the response time is less then 200ms

pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});

Script 2: This will check whether status code is 200

pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});

Step 3: Click on Save

Step 4: When you hover, on the name of your collection, 3 dots will appear. Click on those 3 dots, and then click on “Add new request”

z159

Step 5: Now You can simply paste the API in the space provided and select the API type you are requesting from the dropdown like GET,POST, PUT, DELETE etc. Output will be shown in the body with the status code.

API Used:

https://jsonplaceholder.typicode.com/posts/1

Step 6: You can see the Post-execution scripts results in tab Test Results; as shown below

z166

Output:

Recording-2023-12-03-at-101007

Pre-request Scripts

A pre-request script associated with a request will execute before the request is sent. You can write pre-request scripts for your Postman API requests in JavaScript. The Pre request tab allows for any pre- request processing before a request is sent.

Steps to use Javascript with pre-request scripts

Step 1: After downloading and installing the Postman, open the software. Add a new Collection and give it a name like “GFG”. Here, we can see, multiple tabs like Authorization, Pre-request scripts, Tests , Variables.

postman2

Step 2: Click on Pre-request scripts. Now in the right side pane, we can multiple snippets arrived. We can use any of the available snippet or create our own code based on our requirement. Code is written in JavaScript.

z167

Step 3: We will add a pre-request script that will generate random numbers. We will use those random numbers to set a variable ‘id’ that is already stored as a global variable in our environment as shown below

z168

Pre-request Script:

var random=Math.floor(Math.random()*10);
pm.variables.set('id',random)

Step 4: Click on Save

Step 5: When you hover, on the name of your collection, 3 dots will appear. Click on those 3 dots, and then click on “Add new request”

z159

Step 6: Now You can simply paste the API in the space provided and select the API type you are requesting from the dropdown like GET,POST, PUT, DELETE etc. Output will be shown in the body with the status code.

We use global variable in our request using {{id}}

API USED:

https://reqres.in/api/user/{{id}}

Output:

Recording-2023-12-13-at-114735


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads