问题
If request is using an environment variable then when using pm.request.url.toString() inside a Pre-requisite Script it outputs the query with the placeholder and not it's actual value. meaning it will output https://somesite.com/api/v3/{{env_variable}} instead of https://somesite.com/api/v3/liststuff or whatever.
Is there anyway to get the URL with the value and not the placeholder?
回答1:
Request is not resolved in the pre-request script since the variables could be further modified there. But you can use the postman-collection library within the scripts to resolve the request yourself.
Thanks harryi3t for posting this script on GitHub
https://github.com/postmanlabs/postman-app-support/issues/3322
Here's a sample script
let sdk = require('postman-collection'),
newRequest = new sdk.Request(pm.request.toJSON()),
resolvedRequest = newRequest.toObjectResolved(null, [pm.variables.toObject()], { ignoreOwnVariables: true });
// prints the resolved request to console. Please check DevTools to see the structure
console.log({ resolvedRequest });
Kindly refer screenshot for the same in which custId takes value from placeholder and resolved in pre request also before excuting actual request
来源:https://stackoverflow.com/questions/65173407/pm-request-url-tostring-with-value-of-env-var-instead-of-the-placeholder