Set In Postman Collection BaseURL

1.7k Views Asked by At

Can I create a baseURL in collection level in postman, and in the requests just put the path?

For example I have a Customer Collection (Folder) Inside there is a GET/ customers

instead of writing in the customer collection, in each request the baseURL I want to declare it the collection, and in the GET/ customers, Where I put the url Just put the path ( /customers )

3

There are 3 best solutions below

0
mikee On BEST ANSWER

pm.request.url returns a object with fields host, path, and params .

You can replace the whole object with a string or the individual elements.

An example using Postman Echo.

enter image description here

Then in the pre-request script (which you can set at the request, folder or collection level).

pm.request.url.protocol = "https";
pm.request.url.host = "postman-echo.com";

The resulting console log where you can see that the protocol and host have been transposed appropriately.

enter image description here

1
S.Mishra On

There are multiple ways you can get it done.

Option 1: Create new Environment and add an environment variable with base url.

Option 2: Use Collection's Pre-Request Script to set environment variable for the current selected environment.

Option 2 Steps

  • Lets say the environment variable name is base-url. enter image description here
  • And use the environment variable in request textbox as {{base-url}} and it will get the value from the environment variable called base-url. enter image description here
  • When you hit Send button, the Pre-request script runs first and stores the base-url variable to environment. After that it will be picked by Postman and it will replace {{base-url}} with the value at runtime. It all happens in runtime.
  • You can click on EYE icon next to environment dropdown at right top corner and you can see base-url would be created after clicking on Send.

Personally, I would like to handle it with Option 1 step because then I can clone the environments for different env such as Dev, Test, UAT etc.

0
Midori On

to set the base URL only once, Click on the top folder , which is above api like on FrontAPI if below is structure
FrontAPI
   ----api
     ----v2
       ----xfront

Then select tab pre-request scripts and add the URL there as mentioned by S.Mishra

This will make baseUrl Available to all the requests under that folder