Greetings!
I have learned a lot about how awesome Postman is over the past few days, however, I am now stuck with an issue I can not resolve. I would greatly appreciate a nudge in the right direction?
Setup:
In a Pre-request Script I have a call to a function that returns a Promise wrapped around a pm.sendRequest
. In the result I receive the response object, pull a value off and attempt to store it in a collection variable. In the Request URI I then echo/use said collection variable.
Problem:
The collection variable just won’t save (well, if I do a .get
straight after the .set
it is there). I’ve added in console.log
s to try and see whether everything is happening in order and it appears that it is. However, the Request URI still doesn’t contain/echo the collection variable specified.
Code:
The Pre-request Script…
(async () => {
try {
const subscription = await utils.createSubscription();
pm.environment.set('subscription_id', subscription.id);
} catch (error) {
console.error(error);
}
})();
The createSubscription
function…
const createSubscription = () => {
return new Promise((resolve, reject) => {
pm.sendRequest({
url: `${api_endpoint}/subscriptions`,
method: 'POST',
header: {
'Content-Type': 'application/json',
Authorization: pm.globals.get('jwt_token'),
},
body: {
mode: 'raw',
raw: JSON.stringify({
param1: "123",
param2: "123",
param3: "123"
})
}
}, (err, res) => {
if (err) reject(err);
else {
resolve(res.json().id);
}
}
});
});
};
Could it be a scope issue? I have passed pm
into the anonymous function with no luck. Is it an async timing issue?
Thank you for your consideration