How to use JavaScript in Postman ?
Last Updated :
31 Jul, 2024
Postman is an API(application programming interface) development tool that helps to build, test and modify APIs. In this tutorial, we will see how to use JavaScript in Postman.
Prerequisites:
We will discuss the following two methods to use JavaScript in Postman.
Postman provides a feature for writing JavaScript code for requests, collections, and pre-request or post-request scripts. This helps you to manipulate request and response data, set dynamic variables, perform validations, and so on.
Steps to use JavaScript with Post-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.

Step 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.

We will write 2 snippets in Javascript from the already available snippets from the right-hand side pane.
First Script : 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);
});
Second Script: 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"

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

Output
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.

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.

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

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”

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:
Similar Reads
How to use Native JavaScript Promises in Postman ?
Postman is a popular tool used by developers for testing APIs. While Postman primarily offers a graphical user interface (GUI) for making HTTP requests, it also supports scripting capabilities using JavaScript. One common use case for scripting in Postman is making asynchronous requests and handling
3 min read
How to use Ejs in JavaScript ?
EJS or Embedded Javascript Templating is a templating engine used by Node.js. The template engine helps to create an HTML template with minimal code. Also, it can inject data into the HTML template on the client side and produce the final HTML. Installation StepsInstall the module using the followin
3 min read
How to take user input in JavaScript?
Interacting with users is the best way to create engaging and dynamic web applications. Taking user input allows your applications to be interactive and responsive. Here we will see the approach to take user input in JavaScript, specifically using the prompt method, which is perfect for beginners. A
3 min read
How to use Particle.js in JavaScript project ?
Particles.js is a lightweight JavaScript library used for creating particles that look like the vertices of a polygon. We can also interact by hovering over the particles and creating more particles by clicking on particles. We can use this library in our portfolios which will attract many users and
2 min read
How to Run JavaScript in Visual Studio?
To run JavaScript in Visual Studio, you can either use Node.js in the Terminal or the Code Runner extension. Both methods allow you to execute JavaScript code easily and efficiently within the Visual Studio environment. Using Node.js in TerminalNode.js is a JavaScript runtime that allows you to exec
2 min read
How to use Newman with Postman ?
Newman is a command-line tool that lets you run your Postman collections directly from your computer's command prompt. It extends the functionality of Postman by enabling the execution of these collections in a non-graphical environment. it lets you automatically test your APIs, which is a huge time
3 min read
How to Parse JSON Data in JavaScript?
To parse JSON data in JavaScript, you can use the JSON.parse() method. This method converts a JSON string into a JavaScript object, making it easier to work with the data. 1. Parse Simple JSON Strings[GFGTABS] JavaScript //Driver Code Starts{ const jsonS = '{"name": "Rahul",
2 min read
How to ping a server using JavaScript ?
Pinging a server is used to determine whether it is online or not. The idea is to send an echo message to the server (called ping) and the server is expected to reply back with a similar message (called pong). Ping messages are sent and received by using ICMP (Internet Control Messaging Protocol). T
3 min read
How to Count Records in JSON array using JavaScript and Postman ?
In this article, we are going to explore how JavaScript and Postman are used to count the number of records returned in a JSON array. Send a Request: Send a request to fetch the JSON array from your API endpoint in Postman.Access JSON Response: In the Postman test scripts tab, you can access the JSO
2 min read
How JavaScript Works?
JavaScript is a dynamically typed, cross-platform threaded scripting and programming language, used to put functionality and interactivity at the client side as well as to write logic on the server side of a website. It can display content updates, interactive maps, control multimedia, interactive f
14 min read