# Using Post-request script to update/reset request body

**URL:** <https://community.postman.com/t/using-post-request-script-to-update-reset-request-body/68953>\
**Category:** Help Hub\
**Tags:** scripts, update, iterations, body-parameters, request-body\
**Created:** [September 27, 2024, 12:16am UTC](https://community.postman.com/t/using-post-request-script-to-update-reset-request-body/68953 "2024-09-27T00:16:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![valentin.gauthier](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/valentin.gauthier/32/30246_2.png) [@valentin.gauthier](https://community.postman.com/u/valentin.gauthier)\
**Post date:** [September 27, 2024, 12:16am UTC](https://community.postman.com/t/using-post-request-script-to-update-reset-request-body/68953/1 "2024-09-27T00:16:16Z")

</div>

Hello,

I have a collection with 3 requests on which the user should loop to upload files on a server (1 iteration per file).

One of my request has a “binary” body. To avoid errors if the user forgot to change the file to upload, I want to “reset” the body of this request if the upload went well (in the **post-response** script).

I tried these instructions but all of them failed :

```auto
pm.request.body.update({
    file: {
        src: ""
    }
});
pm.request.body = null;
pm.request.body = "";
pm.request.body.file = null;
pm.request.body.file = "";
pm.request.body.file.src = null;
pm.request.body.file.src = "";

```

Does someone has any solution ?

---

<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:** [September 27, 2024, 9:11am UTC](https://community.postman.com/t/using-post-request-script-to-update-reset-request-body/68953/2 "2024-09-27T09:11:48Z")

</div>

I think that update is temporary. It only affects the body for that iteration\request.

Therefore having it change the body in the post-response script won’t really do anything and the body will be back the way it was on the next iteration of the loop.

I think you need to set a variable that records if the upload went well in the post-response script, and then use that variable in an IF statement in a pre-request script instead and change the body there instead.

---

<div class="post-metadata">

**Author:** ![valentin.gauthier](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/valentin.gauthier/32/30246_2.png) [@valentin.gauthier](https://community.postman.com/u/valentin.gauthier)\
**Post date:** [September 27, 2024, 11:59am UTC](https://community.postman.com/t/using-post-request-script-to-update-reset-request-body/68953/3 "2024-09-27T11:59:13Z")

</div>

Thank you for your response. In deed I also suppose the change will be applied to the request “instance” and not the “stored” request.

I don’t want to change the body in the pre-script in fact. My need is to avoid duplicate sending.

But if there is no real solution, your idea gives me another. I could store a “cache” variable with the file path in the post-script when the request went well. And in the pre-script, make a test on the current file path, if it is the same than the cached one, throw an error.

---

<div class="post-metadata">

**Author:** ![gwynethllewelyn](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gwynethllewelyn/32/30275_2.png) [@gwynethllewelyn](https://community.postman.com/u/gwynethllewelyn)\
**Post date:** [September 30, 2024, 6:02pm UTC](https://community.postman.com/t/using-post-request-script-to-update-reset-request-body/68953/4 "2024-09-30T18:02:08Z")

</div>

AFAIK, each request will launch a different sandboxed VM; therefore, everything is lost after the request terminates, _except_ whatever you’ve explicitly stored on variables outside the scope (e.g., on the environment, for example).

To me, therefore, your suggestion would make a lot of sense!

If you wish to go even further, consider to store not merely the _last_ request made, but, rather, a substantial amount of them, using an array of image URLs 🙂

---

<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:** [October 30, 2024, 6:02pm UTC](https://community.postman.com/t/using-post-request-script-to-update-reset-request-body/68953/5 "2024-10-30T18:02:27Z")

</div>

Closed on October 30, 2024, 6:02pm UTC

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