简体   繁体   中英

How can I create a dynamic variables in Postman for further access?

I log in, then get the token . I stored that varibale and use it throughout my next collections.

let location = pm.response.json().location
let token = location.split("?token=")[1]
console.log('Token : ',token );

When I get the token , I can use that token to call my next API call.

Let's say I call : cpe profiles, I will get a list of profiles in my CPE

{
    "id": "32341696-80c4-4597-998b-5f6cbe7d5a48",
    "group": "a5dd9ed2-f9c6-4d23-9623-de0cae4d4aa8",
    "name": "Jane",
    "dob": null,
    "image": "assets/profile/e6398f955035eb1a6a297739fedcc770.png",
    "cpe": "E8DE2799DD87"
}

I want to create dynamic variables for all my profiles "{{profile.name}}_ID"

1 profile, I want to create 2 variables.

JANE_ID = "32341696-80c4-4597-998b-5f6cbe7d5a48"
JANE_GROUP = "a5dd9ed2-f9c6-4d23-9623-de0cae4d4aa8"

Then, I can access that avarable later on

How can I create a dynamic variables in Postman ?

I think I just solved my owned problem. 👨🏻‍💻😁

var jsonData = JSON.parse(responseBody);
jsonData.forEach(function(object) {
    pm.globals.set((object.name + "_ID").toUpperCase(), object.id);
    pm.globals.set((object.name + "_GROUP").toUpperCase(), object.group);
});

console.log(pm.globals);

在此处输入图片说明

Not exactly dynamic - you can create different environments to hold different values for your user_id.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM