pm.request.url.toString() with value of env var instead of the placeholder

ⅰ亾dé卋堺 提交于 2021-02-11 14:21:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!