Open In App

Postman pre-request-script URL not defined

Last Updated : 27 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Postman is an API (application programming interface) development tool that helps to build, test and modify APIs. It can make various types of HTTP requests (GET, POST, PUT, PATCH), save 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 create a URL in the pre-request script in Postman.

Prerequisites

What is the pre-request script?

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.

What is the Postman pre-request-script URL not defined?

Let’s say, you have a pre-request script to fetch the URL.

var pathName = url.parse(request.url).pathname;
console.log(pathName);
console.log("GeeksforGeeks")

This script will give you errors like pre-request-script URL not defined as shown below

pre-request-script URL not defined

This is because we missed importing the library ‘url ‘ example. We need to import like

const url = require(‘url’);

Steps to create a URL in the pre-request script 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, and Variables.

Add a new Collection

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 snippets or create our code based on our requirements. The code is written in JavaScript.

Pre-request scripts

Step 3: Add a pre-request script, in the space provided.

const url = require(‘url’);var pathName = url.parse(request.url).pathname;console.log(pathName);console.log(“GeeksforGeeks”)

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

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. The output will be shown in the body with the status code.

Output

Recording-2023-12-06-at-004143

Conclusion

Postman has support for APIs, and some of them are pre-included. CryptoJS, for example, is pre-included, hence you’d not need to add it explicitly. The pre-request script has also support for several node modules, to make them work, Postman documentation stated. To use a library, simply call the required function pass the module name as a parameter and assign the return of the function to a variable.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads