Table of contents
1.
Introduction
2.
What are Scripts in Postman?
2.1.
Postman Sandbox
3.
How to create Scripts?
4.
Execution order of Scripts
5.
Frequently asked questions
5.1.
For what purpose is Postman used?
5.2.
What is GET request in Postman?
5.3.
What is an endpoint in Postman?
6.
Conclusion
Last Updated: Mar 27, 2024

What are Scripts in Postman?

Author Neha Chauhan
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Postman is an API development platform for designing, building and testing API (Application Programming Interface). It can create different HTTP requests (GET, POST, PUT), save environments for later use, and convert the API to code for various languages (like Javascript, Python). 

What are scripts img

Scripts is a piece of code that can be executed at a specific point of time in the Postman testing environment. It makes the whole testing process dynamic. Scripts in Postman are written in Postman Sandbox. 

In this article, we will discuss what are scripts and how to create them.

What are Scripts in Postman?

Let’s suppose you are sending a request which requires a security token that is retrieved from a third-party server. It could not be persisted with the environment/global variables as its value changes with every request execution. For this, we can use pre-request scripts.

Scripts img

Scripts is a piece of code that can be executed at a specific point of time in the Postman testing environment. There are two types of scripts -

⭐ Pre-request Scripts: As the name suggests, these scripts are used to execute Javascript before the request runs. 

⭐ Test Scripts: These scripts are executed during the testing environment. 
 

You can write tests, change parameters and even pass data between the request using scripts. A script can be added to

⭐ a collection 

⭐ a folder

⭐ a request within a collection

⭐ a request not saved to a collection.

Postman Sandbox

Postman has a powerful runtime based on Node.js. The Postman Sandbox is a JavaScript execution environment in which the scripts are executed. As, it is a Javascript environment all the scripts must be written using Javascript. 

How to create Scripts?

Let’s get our hands dirty and write the pre-request script. 

https://simple-books-api.glitch.me’ is a Simple Book API which allows you to reserve books. 

In this example we will be using ‘https://simple-books-api.glitch.me/books?type=fiction’ request, which is getting all the fiction books. 

🚀 To write pre-request scripts go to the pre-request tab which is below the GET box. 

output
 

🚀 Let’s display a message 'Hey, Ninja Coder! You are running pre-request script' before running any script. 

 

🚀 To do this write a pre-request script - 

console.log('Hey, Ninja Coder! You are running pre-request script') in your pre-request tab. 

output

 

🚀 Now, open your console, which is at the bottom left of your screen. 

output

 

🚀Click on send. 

depicting results

 

✅ In the console screen you can see that the pre-request script was executed before the GET request.

Yay! 🎊 You just learned how to use pre-request scripts. 
 

🚀 Just like pre-request scripts you can create test scripts by writing the code in the ‘Tests’ tab. 

results

🚀 Open the console and then send a request ‘https://simple-books-api.glitch.me/books?type=fiction

 

✅ The console screen shows that the pre-request script was added before the request was sent while the Test script was added after the request was sent.

🎊 You just learned how to use Test Scripts. 

Execution order of Scripts

As mentioned before, there can be individual scripts as well as scripts in a collection. 

individual scripts

 

In postman, for a single request script, the execution order looks like this-


      ⭐ Pre-request script associated with a request will execute before the request is
       sent.


      ⭐ A test script associated with a request will execute after the request is sent.

In the collection, the request script execution order looks like this-

     ⭐ A pre-request script associated with a collection will run prior to every request in 
      the collection.


     ⭐ A pre-request script associated with a folder will run prior to every direct child
      request in the folder.


     ⭐ A test script associated with a collection will run after every request in collection.


     ⭐ A test script associated with a folder will run after every direct child request in the
     folder.

execution order

Frequently asked questions

For what purpose is Postman used?

Postman is an API client that makes it easy for developers to create, share, test and document APIs. Users are allowed to create and save simple and complex HTTP/s requests, as well as read their responses.

What is GET request in Postman?

A GET request gets the information from the server. The server responds to the GET request without affecting any data on the server, that is, there is no creation, updation, addition, or deletion of data on the server when you are making a GET request.

What is an endpoint in Postman?

An API endpoint is a point at which an API connects with the software program. APIs work by sending requests for information from a web application or web server and receiving a response.

Conclusion

We are so proud of you for finishing this article🤩. In this article, we discussed what are scripts. The two types of scripts are pre-request and test scripts. Then we discussed how to create these scripts by taking an example of both pre-request and test scripts.

After reading this article, if you are interested in knowing more about Postman, we recommend you to read some of our articles -

🔥 Web Testing

🔥 API

🔥 API Testing

You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations, and much more!!

We wish you Good Luck!🎈 Please upvote our blog 🏆 and help other ninjas grow.

Live masterclass