Using Post-request script to update/reset request body

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 :

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 ?

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.

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.

1 Like

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 :slight_smile:

1 Like