I am having multiple pm.sendRequest() that are being kept in pre-request tab of a request.
My concern is that not able to send the requests as per order.
For example.Login should be first,then job creation,then task creaion like that…
When tried to send a request which contains all the pm.sendRequest(),login ,job creation,task creation request are getting collasped in execution order.
Is there way to keep run the requests in a order…
Hey @spacecraft-technol17
The requests are async and could be sent in any order.
This is a frequently asked question and @singhsivcan did some great work with providing a potential solution for your workflow:
I was working with a colleague and we were trying to achieve something by chaining multiple requests as a pre-step before the actual request happened.
Use case:
We wanted to call 3-4 specific endpoints in a specific order and gather all the required data before the actual request was sent out.
One way was to do it by creating multiple requests and using the collection runner. But the other way was to make use of pm.sendRequest, however, there’s no guarantee in which order multiple of pm.sendR…
There’s also an example of these in this Public Workspace
https://www.postman.com/postman/workspace/postman-answers/collection/3407886-220af2f6-63f2-4d84-903d-99e6e296a8c8?action=share&source=copy-link&creator=8213448
1 Like
Thanks for your reply…Now i am happy as my issues are solved
1 Like
Hi Danny, isn’t that a workaround from before the promise support was fixed in Postman. (Sometime in 2023).
I did try searching for a new example but couldn’t find one.
So here is my effort.
First is a showcase of the issue using a loop to send multiple requests to Postman Echo.
We are updating the body of the request on each iteration to include the iteration number.
for (let i = 1; i < 11; i++) {
let testValue = `request${i}`
let request = {
url: 'https://postman-echo.com/get?test=' + testValue,
method: 'GET',
}
pm.sendRequest(request, (err, res) => {
if (err) {
console.log(err);
}
pm.test(`Request ${i} - status code is 200`, () => {
pm.expect(res).to.have.status(200);
let resJson = res.json();
console.log(resJson.args.test);
});
});
The console logs then show that the requests (and tests) are not going sequentially due to the async nature of sendRequest().
Therefore, we are going to update our code to use Promises and awaits.
The await will stop processing any more code until the promise has been resolved. Please note: awaits only work in async functions.
const sendRequest = (req) => {
return new Promise((resolve, reject) => {
pm.sendRequest(req, (err, res) => {
if (err) {
console.log(err);
return reject(err);
}
resolve(res);
});
});
};
(async function main() {
for (let i = 1; i < 11; i++) {
let testValue = `request${i}`
let request = {
url: 'https://postman-echo.com/get?test=' +testValue,
method: 'GET',
}
const response = await sendRequest(request);
pm.test(`Request ${i} - status code is 200`, () => {
pm.expect(response).to.have.status(200);
let resJson = response.json();
console.log(resJson.args.test);
});
};
})();
With the console logs and tests now being in the expected order.
You’re probably referring to this one right?
Hello Postman team,
We are using Postman to automate a broad number of requests. Sometimes we need to query an API programmatically, wait for its response, process it, and then proceed depending on what we have received. Most of the time these types of checks and requests are best made programmatically and do not warrant another request to be made in the collection as it is to be considered a dummy request and right now we have a lot of them for other reasons, so adding more is not an option as…
If either of the solutions work for @spacecraft-technol17 , it’s all good and gets them unblocked.
1 Like
Not exactly that one, but its the same topic and I’ve amended my code slightly based on that topic.
1 Like
system
(system)
Closed
July 6, 2024, 1:04pm
7
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.