< Back

How to setup a Pre-request script in postman?

Author

Koen Van Belle

Date

31/10/2019

Share this article

Pre-request scripts documentation is lacking

We all know Postman as an API ‘development/testing’-tool, specifically designed for developers. I have seen developers use the environment and global variables feature. Yet we all seem to ignore the possibility to write Pre-Request scripts. The documentation is unclear or too complicated. And I agree, the docs on fully fledged Pre-Request scripts is lacking. So let’s fix that, shall we? 

How does a pre-request script work?

The Pre-Request Script tab in postman allows us to specify actions to be taken before the actual request, ranging from setting a certain variable or performing a prior-call. Postman will execute any javascript written in that tab before anything else. Take note that this is an option on collection level and request level. So the order of execution must be taken into account: 

  1. Collection Pre-Request Script 

  2. Request Pre-Request Script 

  3. The Actual Request 

Hands on pre-request scripting.

I’ll use the petstore swagger as an example. As the Pre-Request script will create (POST) a pet and use the actual call to GET the newly created pet back. The function to send a Request in the Script is the following: 

pm.sendRequest(createRequest, (err, res) => {}); 

As you can see, I am referring to a variable in the function called createRequest. It is perfectly possible to specify everything you need inside the function itself, yet I find that this reduces the readability too much. So we have to declare a variable. 

const createRequest = { 
    url: 'https://petstore.swagger.io/v2/pet', 
    method: 'POST', 
    header: { 
        'content-type' : 'application/json' 
    }, 
    body: { 
        mode : 'raw', 
        raw : JSON.stringify({ 
            "id": 1, 
            "category": { 
                "id": 23, 
                "name": "Mammal" 
            }, 
            "name": "Parpa", 
            "photoUrls": [ 
                "string" 
            ], 
            "tags": [ 
            { 
                "id": 0, 
                "name": "string" 
            } 
            ], 
            "status": "Dead" 
            }) 
}}; 

A few things to pay attention to here, it is required to specify the content-type in the header. The body has the same requirement, for which the property mode is used. The JSON.stringify is needed due to javascript. Otherwise you will be sending an object and not a JSON. 

Maintainability

In order to make the Script more maintainable, let’s quickly add another variable. 

let petId = Math.floor(Math.random() * 100) + 1; 
pm.environment.set("petId", petId) 

This will allow us to call the petId in our const createRequest and in the following call. 

GET https://petstore.swagger.io/v2/pet/{{petId}} 

Result: the full code

let petId = Math.floor(Math.random() * 100) + 1; 
pm.environment.set("petId", petId) 
 
const createRequest = { 
    url: 'https://petstore.swagger.io/v2/pet', 
    method: 'POST', 
    header: {  
        'content-type' : 'application/json' 
    }, 
    body: { 
        mode : 'raw', 
        raw : JSON.stringify({ 
            "id": petId, 
            "category": { 
                "id": 23, 
                "name": "Mammal" 
            }, 
            "name": "Parpa", 
            "photoUrls": [ 
                "string" 
            ], 
            "tags": [ 
            { 
                "id": 0, 
                "name": "string" 
            } 
            ], 
            "status": "Dead" 
            }) 
}}; 
 
pm.sendRequest(createRequest, (err, res) => {}); 
GET https://petstore.swagger.io/v2/pet/{{petId}} 

Enjoy and happy testing! 

References