# Use Existing Postman Request in Pre/Post Script

**URL:** <https://community.postman.com/t/use-existing-postman-request-in-pre-post-script/23477>\
**Category:** Help Hub\
**Created:** [May 6, 2021, 3:25pm UTC](https://community.postman.com/t/use-existing-postman-request-in-pre-post-script/23477 "2021-05-06T15:25:44Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![cryosat-candidate-16](https://avatars.discourse-cdn.com/v4/letter/c/e9c0ed/32.png) [@cryosat-candidate-16](https://community.postman.com/u/cryosat-candidate-16)\
**Post date:** [May 6, 2021, 3:25pm UTC](https://community.postman.com/t/use-existing-postman-request-in-pre-post-script/23477/1 "2021-05-06T15:25:44Z")

</div>

Hi,

I scoured the docs and the internet to see if this is possible but was unable to find anything. Is there a way to use an existing Postman request that’s defined within the same collection or different collection in a pre/post script? I know there’s a `pm.sendRequest` function, but it just strikes me as inefficient to have to define the request again.

Thanks,  
Quy

---

<div class="post-metadata">

**Author:** ![sean.keegan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/sean.keegan/32/6353_2.png) [@sean.keegan](https://community.postman.com/u/sean.keegan)\
**Post date:** [May 6, 2021, 3:45pm UTC](https://community.postman.com/t/use-existing-postman-request-in-pre-post-script/23477/2 "2021-05-06T15:45:42Z")

</div>

Hi @cryosat-candidate-16! Welcome to the community forum! 🏆

I think what you be looking for is the `setNextRequest` method. You can use this to programmatically set the next request based on your own conditional logic in the Pre-request or Tests tab. [Here’s](https://learning.postman.com/docs/running-collections/building-workflows/) a bit more information on controlling your workflow within Postman.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![cryosat-candidate-16](https://avatars.discourse-cdn.com/v4/letter/c/e9c0ed/32.png) [@cryosat-candidate-16](https://community.postman.com/u/cryosat-candidate-16)\
**Post date:** [May 6, 2021, 4:16pm UTC](https://community.postman.com/t/use-existing-postman-request-in-pre-post-script/23477/3 "2021-05-06T16:16:42Z")

</div>

Thanks @sean.keegan for the reference and the prompt response.

I looked at the docs and it looks like it sets the workflow and you can’t make use of the response in a pre-script. My use case here is that I have to auth against a login endpoint and then use the generated token as in an auth header for another request. I have to do this for all requests for a particular REST resource. I would like to just automate by defining a collection pre script that is executed before each call, calls the auth endpoint, grabs the token and sets the token as a collection ENV var. My other requests would then use that ENV var to set its auth headers.

It’s not a big deal if it’s not possible as I can just accomplish this through the sendRequest function.

---

<div class="post-metadata">

**Author:** ![sean.keegan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/sean.keegan/32/6353_2.png) [@sean.keegan](https://community.postman.com/u/sean.keegan)\
**Post date:** [May 6, 2021, 5:00pm UTC](https://community.postman.com/t/use-existing-postman-request-in-pre-post-script/23477/4 "2021-05-06T17:00:09Z")

</div>

Ah okay! I think what you may want to do is take advantage of the pre-request script at the collection level then. Anything in the pre-request script of the collection level folder will run before each and every request in the collection.

If I’m understanding your need and use case correctly, one of our Postman community superstars @praveendvd has demonstrated something similar to this in his public workspace. [Check it out here](https://www.postman.com/praveendvd-public/workspace/postman-tricks-and-tips/collection/8296678-fd74eeed-c490-43c0-bbba-6f0faea0ee1d?ctx=documentation) and see if this is something that might be useful for what you’re looking for - specifically look at the pre-request tab of the collection level folder.

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [May 6, 2021, 5:23pm UTC](https://community.postman.com/t/use-existing-postman-request-in-pre-post-script/23477/5 "2021-05-06T17:23:57Z")

</div>

@sean.keegan🤭 thanks sean for the kind words 💖 happy to see the public workspace being mentioned .

@cryosat-candidate-16 just store the request you want as a variable

In the test script of the req you want to send in prerequest add:

```
   pm.environment.set("req", pm.request)

```

And in pre-request script where u want to send it use;

```
  pm.sendRequest(pm.environment.get("req"))
```

---

<div class="post-metadata">

**Author:** ![cryosat-candidate-16](https://avatars.discourse-cdn.com/v4/letter/c/e9c0ed/32.png) [@cryosat-candidate-16](https://community.postman.com/u/cryosat-candidate-16)\
**Post date:** [May 6, 2021, 7:32pm UTC](https://community.postman.com/t/use-existing-postman-request-in-pre-post-script/23477/6 "2021-05-06T19:32:09Z")

</div>

Thanks to the both of you. Saving the request object in an env worked.

---

<div class="post-metadata">

**Author:** ![felipe.ferreira1003](https://avatars.discourse-cdn.com/v4/letter/f/3be4f8/32.png) [@felipe.ferreira1003](https://community.postman.com/u/felipe.ferreira1003)\
**Post date:** [January 5, 2022, 8:26pm UTC](https://community.postman.com/t/use-existing-postman-request-in-pre-post-script/23477/7 "2022-01-05T20:26:44Z")

</div>

Thanks for the solution.

This will help me a lot.

---

<div class="post-metadata">

**Author:** ![atifqa-ali](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@atifqa-ali](https://community.postman.com/u/atifqa-ali)\
**Post date:** [June 3, 2022, 12:26pm UTC](https://community.postman.com/t/use-existing-postman-request-in-pre-post-script/23477/8 "2022-06-03T12:26:36Z")

</div>

@praveendvd  
Can we set the pre-request in a variable?  
If yes, then how?  
Please guide.

Thanks.

---

<div class="post-metadata">

**Author:** ![science-geoscienti16](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@science-geoscienti16](https://community.postman.com/u/science-geoscienti16)\
**Post date:** [August 13, 2023, 6:44am UTC](https://community.postman.com/t/use-existing-postman-request-in-pre-post-script/23477/9 "2023-08-13T06:44:35Z")

</div>

> [@praveendvd](#):
>
> ` pm.environment.set("req", pm.request)`

Hi friend, if I want run many request in a folder, how to resolve it? I can’t find answer in google. Could you plz help me this problem? Many thanks!

---

<div class="post-metadata">

**Author:** ![kevinc-postman](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kevinc-postman/32/12774_2.png) [@kevinc-postman](https://community.postman.com/u/kevinc-postman)\
**Post date:** [August 14, 2023, 7:32pm UTC](https://community.postman.com/t/use-existing-postman-request-in-pre-post-script/23477/10 "2023-08-14T19:32:11Z")

</div>

Hi @science-geoscienti16 !

Aside from running your folder via the collection runner in Postman, you can chain these requests with `postman.setNextRequest("collection name or ID")` individually.

Postman does not otherwise support running an entire folder programmatically in this way.

---

<div class="post-metadata">

**Author:** ![ceepan](https://avatars.discourse-cdn.com/v4/letter/c/e36b37/32.png) [@ceepan](https://community.postman.com/u/ceepan)\
**Post date:** [November 3, 2023, 4:14pm UTC](https://community.postman.com/t/use-existing-postman-request-in-pre-post-script/23477/11 "2023-11-03T16:14:34Z")

</div>

Is it possible to programmatically retrieve a list of requests names to use with setNextRequest?

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [November 3, 2023, 6:11pm UTC](https://community.postman.com/t/use-existing-postman-request-in-pre-post-script/23477/12 "2023-11-03T18:11:10Z")

</div>

@ceepan

This should have been a new question (as this question has been answered).

Have a look at the following topic.

[List of all requests in pre-request script - 🙋 Help - Postman Community](https://community.postman.com/t/list-of-all-requests-in-pre-request-script/17612)

You could use this in a pre-request script or in the tests tab by sending the same parameters via sendRequest.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [February 1, 2024, 6:11pm UTC](https://community.postman.com/t/use-existing-postman-request-in-pre-post-script/23477/13 "2024-02-01T18:11:28Z")

</div>

Closed on February 1, 2024, 6:11pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
