What is equal Bruno command or option for pm.sendRequest with call back? #4732
KaliswaranNachimuthu
started this conversation in
General
Replies: 2 comments 10 replies
-
Bruno's try {
const data = await bru.sendRequest('my-request');
console.info(data);
// Do something with the data like
} catch (error) {
console.error(error);
} |
Beta Was this translation helpful? Give feedback.
4 replies
-
So how does the new console.log works but setting a global variable in the callback does not work: bru.sendRequest({
method: 'POST',
url: 'https://postman-echo.com/post',
data: {"hello": "world"}
}, function (err, res) {
if (err) {
console.error('Error:', err);
return;
}
console.log(res.data.data.hello); // works
bru.setGlobalEnvVar('gTest', res.data.data.hello); // does not work
}); workaround is to await bru.sendRequest({
method: 'POST',
url: 'https://postman-echo.com/post',
data: {"hello": "world"}
}, function (err, res) {
if (err) {
console.error('Error:', err);
return;
}
console.log(res.data.data.hello); // works
bru.setGlobalEnvVar('gTest', res.data.data.hello); // works
}); or is it better just to forego callback? const rs = await bru.sendRequest({
method: 'POST',
url: 'https://postman-echo.com/post',
data: {"hello": "world"}
});
console.log(rs.data.data.hello); // works
bru.setGlobalEnvVar('gTest', rs.data.data.hello); // works |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to find options for pm.sentRequest with callback. I am not finding exact info on the discussion or issues. I use the below script in postman pre request script to send the request and get the required data for the current script. However, I am not finding this option in Bruno.
const getRequestUrl =
${pm.environment.get("applicationDomain"
)}/api/${pm.environment.get("API_VERSION")}/accounts/`;
const accountRequest = {
url: getRequestUrl,
method: "GET",
};
const setCSRFToken = (headers) => {
const cookieHeader = headers.filter((h) => {
return h.key === "Set-Cookie" && h.value.startsWith("x-TOKEN");
});
const ca = cookieHeader[0].value.split(";");
pm.environment.set("CookieToken", ca[0].substring(11));
};
pm.sendRequest(accountRequest, function (err, firstResponse) {
setCSRFToken(firstResponse.headers);
const body = firstResponse.json();
const assetAccounts = body.assetAccounts.assetAccount.map((a) => {return {...a.accountId, id: a.id}});
const liabilityAccounts = body.liabilityAccounts.liabilityAccount.map(
(l) => {return {...l.accountId, id: l.id}}
);
const accounts = [...assetAccounts, ...liabilityAccounts];
pm.environment.set("accounts", JSON.stringify(accounts));
pm.environment.set("accountHistoryNextIndex", 0);
pm.environment.set("accountDetailsNextIndex", 0);
pm.environment.set("accountId", accounts[0].id);
});
`
Beta Was this translation helpful? Give feedback.
All reactions