Sunday, 23 July 2023

Replace in postman java script | replace domain name with ip in url and then set as environment variable

Let us assume a POST response is returning a URL in header.  

Now, how to replace the DNS name with IP address and set the URL as environment variable ? 

Below postman java script does the job. 

The first var line(var siteurl) gets the URL from the POST response. 
The second var line(var ip) gets the IP from environment variable , 
The third var line(var location)  replaces the DNS name with IP and then 
The fourth var line sets the new URL that is replaced from third line as environment variable(location). 

var siteurl = pm.response.headers.get("location");  // location= https://www.this-is-amazon-site-hostname.com/books
console.log("siteurl="+siteurl) //	https://www.this-is-amazon-site-hostname.com/books

var ip = pm.environment.get("serverip"); //serverip is the environment variable
console.log("Server IP="+ip)  	//   10.123.4.23

var location = siteurl.replace("www.this-is-amazon-site-hostname.com", ip);
console.log("location="+location) // https://www.10.123.4.23/books

pm.environment.set("sitenewurl",location) //sitenewurl is the environment variable and the value will be set to  https://www.10.123.4.23/books



No comments:

Post a Comment