How to print only the hostname from a request url

Hey, I have a use case where i need to store the host URL as variable,
I used few scripts

  1. pm.request.url.host >> this gives output in array format like
    `[“app”, “xyz”, “com”]

  2. 0: “app”

  3. 1: “xyz”

  4. 2: “com”`

Can someone help me to print it like app.xyz.com

@danny-dainton need your help here

Thanks in advance

Hi @swasthik, you can do it using join() method :blush:

Please try the below snippet.

console.log("URL is : " + pm.request.url.host.join('.'));

Please let me know if this is your expectation :slightly_smiling_face:

2 Likes

yes, perfect Thanks @bpricilla :slight_smile:

1 Like

@swasthik Great to know!! Just sharing this link for you to know more about this join method :slightly_smiling_face:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join

You can also use this without using an additional .join() method. :smiley:

console.log(pm.request.url.getHost());

There are a bunch of helper methods available in the auto-suggestion list.

1 Like

Where in postman do I log put these console.log statements and where do I see the results? I put them in Tests and didn’t see anything in Test Results

WhereLogHost

@bencook16

They don’t get recorded in the test results. They are just information, and are useful when debugging your tests, for example checking what has been saved in a variable.

You open the Console from the bottom left-hand corner.

image

The statements can be put in several places. The main one would be the Tests tab for a particular request, but you could also put them in the pre-request scripts or Tests tab at the collection or folder level.

1 Like