21

I have two postman requests x,y which hit endpoints in two different rest api X,Y. x will give me an authentication token which is necessary to make the y request. How do I make the request x inside request y ? That is, call x from inside y.

PS - I have already figured out how to use request x to setup environment variables (auth token) which will be picked up by y.

Postman
  • 211
  • 1
  • 2
  • 4
  • No idea buddy but on StackOverflow you can open bounty if you got some reputation to get more attention (http://stackoverflow.com/?tab=featured). I will answer your question but in another place, because yours is duplicated :( (http://stackoverflow.com/q/39551635/3866610) – Piotr Dawidiuk Feb 01 '17 at 17:22
  • http://stackoverflow.com/a/41986305/3866610 Hope it helps at least a little – Piotr Dawidiuk Feb 01 '17 at 17:48
  • Does this answer your question? [How to run one request from another using Pre-request Script in Postman](https://stackoverflow.com/questions/39551635/how-to-run-one-request-from-another-using-pre-request-script-in-postman) – Henke Jan 25 '21 at 16:26

4 Answers4

9

If you want to do it in one shot, you can use the pre-script option.

Pre-script are triggered before the request. So if you make a request and set the token in an environnement variable, the next call can use the token. You should check that page. It seems outdated, you should replace pm by postman.

You might also want to add a pre script request to your collection. This way, every request of that collection will be precede by a request to the authentification route for exemple.

Alexander
  • 160
  • 1
  • 9
Ernest Jones
  • 564
  • 6
  • 20
  • 2
    I think this is the simplest and best answer. As an additional helping hand - to send a request in a "Pre-request Script" look up "pm.sendRequest" – Stuart Apr 22 '20 at 12:02
8

Have a look at this: https://blog.postman.com/conditional-workflows-in-postman/, if you want to call a specific request from within a request, the postman.setNextRequest() function will do the job.

Alexandre

Henke
  • 4,445
  • 3
  • 31
  • 44
A.Joly
  • 2,317
  • 2
  • 20
  • 25
  • 1
    the link seems dead :/ This one may be containing the same content ? https://blog.postman.com/conditional-workflows-in-postman/ – user1864070 May 05 '20 at 17:39
5

The official documentation has you covered big dog - Branching and looping.

When running a collection, you can branch and loop across API requests in Postman using the postman.setNextRequest("request_name"); function.

Then you use the collection runner to run the API requests in sequence.

agandi
  • 561
  • 7
  • 15
0

Think about using the Collection Runner to execute a sequence of requests.

Xin Meng
  • 1,982
  • 3
  • 20
  • 32
  • 1
    Collection runner is created as a test tool. It will run every request of a collection and execute the tests associated with those. You might be able to trick this option but clearly, it is not the best way to do it. – Ernest Jones Oct 06 '20 at 10:41