邮递员预请求脚本 URL 未定义

Postman pre-request-script URL not defined

我在 Postman 中有一个预请求脚本需要创建 URL:

var uri = new URL(request.url).pathname;
console.log("uri:" + uri);

失败并出现错误:URL 未定义。我四处搜索并尝试了所有不同的方法:

//const url = require('url');
//const URL = require('url').URL;

None 其中有效果。我检查节点版本和 npm,它显示它们已安装

node -v
-bash: node: command not found
npm -v
-bash: npm: command not found

我必须安装节点才能工作吗?

我也运行 chrome 开发人员工具控制台中的代码,与未定义的结果相同:

var uri = new URL(request.url).pathname
undefined

但是在同一个脚本中我也使用了 CryptoJS,它不需要任何导入,它就可以工作。

我使用的是 macOS Mojave 10.14.6 和 POSTMAN 7.21.2 应用程序,而不是 chrome 扩展程序。

Postman 支持 API,其中一些是预先包含的。例如,CryptoJS 是预先包含的,因此您不需要显式添加。预请求脚本还支持多个节点模块,使它们工作,Postman 文档指出:

In order to use a library, simply call the require function and pass the module name as a parameter and assign the return of the function to a variable.

所以,在你的情况下,它应该是这样的:

const url = require('url');
var pathName = url.parse(request.url).pathname;

console.log(pathName);

详细文档:Postman Sandbox API reference