How to read some kind of a value from a request in the collection's pre-request script?

Authentication for requests is done in the collection’s pre-request script. The type of authentication and provider is determined by criteria in each request. The criteria can be a key/value pair. Each request has its own values. Where can I save these values in the request to be read by a parent pre-request script? I don’t want the key/value to be in the url or query param because they are not really part of the outgoing request. They are only useful and are used in the pre-request script.

Bard or ChatGPT suggested to use: pm.request.params.get(‘parametername’) and uncheck the checkbox for the parameter in the Params tab so it doesn’t get added to the url. I said great! Then I found out there’s no such capability and it was just an AI hallucination!

This would have been exactly what I wanted.

Collection or Environment variables??

Everything is in a collection plus I am not sure what you mean by a collection variable. There are global and environment variables plus local variables set in JS in a script.
I also don’t get your question about environment variables. Environment variables typically are not used for specific requests. If I have 1000 requests, it’s not feasible to have 1000 environment variables.

Using variables | Postman Learning Center

Whether its a collection variable, environment variables, global or local.

It has to be stored somewhere.

The above link will explain the scope.

Please don’t use global unless it really is global and required across collections.

Back to the original question.

Each request has its own values. Where can I save these values in the request to be read by a parent pre-request script?

These have to be stored somewhere, so pick a scope. It sounds like you want to use the values in a pre-request script, so then need to be stored as a variable.

Storing them somewhere else, like in a parameter will most likely then break the actual request if those parameters are not needed (as they will get sent).

If the request has a JSON body. You could store the details in the body, retrieve them using the pre-request script and then create a completely new body. That might work.

const body = JSON.parse(pm.request.body.raw);
// do something with the existing body.
// you can create a completely new body at this point if you wanted.
pm.request.body.raw = body;  // this is what will be used in the actual request

I suspect you can do it for other methods, like ‘params’ in a similar way, but you’ll have to work out how to extract and update as I’ve not used that method.

I think you mean put them in the body, read them from the body in the pre-request script and then remove them before the request is sent. That might work but it’s hacky. I put them now in the headers. I don’t care if they go out in there.

I will try to put in a feature request for this functionality. There are times when a parent’s pre-request script needs some meta data about its child’s request without having this data be part of the request itself that goes out. The data is for Postman’s use only

Fair enough if that is what works for you.

My approach would have been to create the JSON, minify it, and then store it as a string in a collection or environment variable.

That would then be available to your pre-request script.

On a side note, where are you storing the other authentication credentials. The ones likely to be sensitive? As I would be storing this in the same place.