hello,
I’m new to this and trying to do a flow where 1 send request will create a variable called orgID from the response it gets and pass it on to the next send request.
the first flow when I run it, generates this below.
[ { “id”: “11223344”, “name”: “somename”, “url”: “https://someurl”, “api”: { “enabled”: true }, “licensing”: { “model”: “co-term” }, “cloud”: { “region”: { “name”: “North America” } } } ]
I want to create a collection variable called myOrg using the tests section and have myOrg have the value 11223344.
this is the tests I have so far but it’s just dumping the entire response back on console starting from id which isn’t what I need, I need myOrg to contain only the value 11223344.
var jsonData = JSON.parse(responseBody);
pm.collectionVariables.set(“myOrg”, pm.response.json.id);
console.log(pm.response.json(“myOrg”));
thanks for any help.
pm.collectionVariables.set(“myOrg”, pm.response.json().id);
console.log(pm.CollectionVariables.get("myOrg"));
the response I get back is “undefined”.
On closer inspection, it looks like you have an array.
const response =
[ { "id": "11223344", "name": "somename", "url": "https://someurl", "api": { "enabled": true }, "licensing": { "model": "co-term" }, "cloud": { "region": { "name": "North America" } } } ]
console.log(response);
pm.test('response is an array', () => {
pm.expect(response).to.be.an('array');
});
console.log(response[0].id)
Therefore the following should work.
const response = pm.response.json()
pm.collectionVariables.set("myOrg", response[0].id);
console.log(pm.CollectionVariables.get("myOrg"));
fantastic,
it gives me “11223344”, how would I remove the " please and get only 11223344?
Postman uses JavaScript under the hood.
const response = pm.response.json()
pm.collectionVariables.set("myOrg", parseInt(response[0].id));
console.log(pm.CollectionVariables.get("myOrg"));