How do I wrap an IP address before passing it along?

Hi guys,

This is my first post here, and I must start by saying I’m not a developer. Even the depth of my copy & paste JavaScript knowledge is rather shallow, so please be understanding.

I am very excited with Postman, as it allows me to get things done that on UI would either be too slow, or unreliable.

I’m trying to pass along an IP address that I get from one API to another.

I get the IP address as a pure text, the response would be:

xxxx:xxxx:xxxx:xxxx...

No quotation marks.

What I’ve been trying to do:

Pass that IP along to a Global Variable {{ipv6}} in a Pre-Script, then send this variable along with other info in a request to another API. I currently have:

pm.globals.set("ipv6", Object.values(response.json()));

Because it’s actually an IPv6, I always get the following error:

Unexpected token ':' at 1:5

I guess I need to somehow wrap the IPv6 address with quotation marks before I pass it on. I tried:

pm.globals.set("ipv6", "Object.values(response.json())");

But got the very same error message.

Thanks

Hey @floripare,

Welcome to the community :wave:

Are you able to provide an example of the response body (mask any sensitive data)? It will help us get you an answer quicker.

For example, if this was the response body:

{
    "ip": "xxxx:xxxx:xxxx:xxxx"
}

Added this to the Tests tab would set that value as a Global variable:

pm.globals.set("ipv6", "pm.response.json().ip")

More information about the pm.* API and the different options can be found here:

https://learning.getpostman.com/docs/postman/scripts/postman_sandbox_api_reference

Hi @danny-dainton,

Thanks for your reply. I was actually using an API that had a response body just like the one you gave as an example. However, there seems to be an issue that is making Postman not receive IPv6, but instead an IPv4. I’ve contacted support about this and they referenced an ongoing issue that is being dealt with.

The API that I’m currently trying to use instead is

https://ipv6bot.whatismyipaddress.com

It returns the correct IPv6, but in plain text, not formatted as JSON:

image

That’s fine, you could still use that to grab the value.

pm.globals.set('ip_address', pm.response.text())

This does work as a Test, but not as a Pre-request script.

pm.sendRequest("https://ipv6bot.whatismyipaddress.com", function (err, response) {
    console.log(response.json())
});
pm.globals.set("my-current-ipv6", pm.response.text());

I’ve tried to insert the pm.globals.set() command in the pm.sendRequest() function, but either way I get the same error:

response-body-error

This works in a slightly different way when used with the pm.sendRequest() function.

You need to do a couple of things, bring the pm.globals into the function and drop the pm. from the value. This will then use the response argument in the function.

pm.sendRequest("https://ipv6bot.whatismyipaddress.com", function (err, response) {
    pm.globals.set("my-current-ipv6", response.text());
});

Using pm.globals.set("my-current-ipv6", pm.response.text()); in a pre-request will fail, this runs before the requests so it doesn’t know what pm.response is, in that context.

Hope that makes sense :slight_smile:

Certain things never make sense to me :blush:

Anyway, that worked! Thank you very much.

I had actually tried exactly this (bringing the pm.globals into the function and dropping the pm., but I was adding the pm.globals.set() line after the console.log(), not instead of it.

2 Likes