In my pre-request script I want to access the full request.url and request.body (backstory is a hash algorithm will be run over those). I am also using environment variables in my request builder. In my pre-request script when I access request.url, it still has the environment variable name rather than the expanded value (Ex: http://{{server}}/endpoint). Is there a way to get the expanded value (Ex: http://dev-api.server/endpoint )? In the test script, I can access the request.url and I do get the expanded value. Also in the pre-request script the request.body is returned as undefined. Same thing, I would like to read the body that is going to be sent so I can pass it into the hash algorithm. Thanks.
Responding to my own as I was able to figure it out. Need to call the .substitute method on the collection of variables that are relevant. I know mine are in environment, not sure what the optimization would be if they are in either environment, collection or globals, but this seems to work:
pm.environment.values.substitute(pm.request.url, null, false);
Documentation at http://www.postmanlabs.com/postman-collection/VariableList.html#substitute
Thanks, this worked for me. But why intelisense is not working when you type pm.environments.values.substitute() in the script tabs? Also, the documentation is not clear so how did you figure out how to use that command?
For anyone wondering how to do this with different variable locations, you can just apply the method multiple times like so:
for environment
let url = pm.environment.values.substitute(pm.request.url, null, false);
for collection
url = pm.collectionVariables.values.substitute(url, null, false);
for global
url = pm.globals.values.substitute(url, null, false)
Then your url variable will have all the variables substituted in where they need to be.
All you would need to use is this, for all scopes:
pm.variables.replaceIn(pm.request.url)
That will substitute any placeholders in the URL, with the resolved values.