postmanpostman-pre-request-script

Set In Postman Collection BaseURL


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 )


Solution

  • 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