rong
rong

Reputation: 121

Postman - execute the first request for the first iteration only with multiple requests * multiple iterations

I get a postman collection contains request A,B,C. When I run with multiple iterations (e.g. 3 iterations), I should ensure the request A is executed only for the first iteration and the request B & C is executed for each iteration, like this:

iteration #0 - request A

iteration #0 - request B

iteration #0 - request C

iteration #1 - request B

iteration #1 - request C

iteration #2 - request B

iteration #2 - request C

I tried postman.setNextRequest() in pre-request of the request A but it cannot skip itself and it only takes effect to next request.

I believe this scenario is very common and a typical example is that Request A generate a long-life token code which will be used by following requests/iterations.

Does anyone have an idea? Appreciated!

Upvotes: 2

Views: 967

Answers (2)

rong
rong

Reputation: 121

  1. remove request A from collection

  2. add this section in pre-request of request B

    if (pm.info.iteration === 0) { pm.sendRequest({ url: 'https://url', method: 'POST', header: { 'Content-Type': 'application/json', }, body: { mode: 'raw', raw: JSON.stringify({ "a": "a_value" }) } }, function (err, res) { postman.setEnvironmentVariable("mytoken", res.json().token); }); }

Upvotes: 0

Abhansh Giri
Abhansh Giri

Reputation: 191

You can try the solution below to see if this solves your problem.

  • Create that long life token or whatever you wanna do once in preRequest script of request B and saves that long life token in environment variables or in general you can just put a flag in environment variables say request_A_ran_already. Let that code be in an if condition which checks the environment if the long life token is already created, if yes, that flow will be skipped. Remember to save that long life token in environment/global variables. You'll need to persist it if you want to do operations based on it later.

Let me know if this works for you, comment if you have any doubt regarding this.

Upvotes: 2

Related Questions