在 Postman 中导入外部库

Importing external libraries in Postman

我的需求: 我想在我的 JSON 响应中构建一个包含过滤器的路径。

//Simple path - OK
response.Node1.Node2.Node3.Node4

//Path with list - OK
response.Node1.Node2.Node3[1].Node4

//Path with condition (filter) - NOK
response.Node1.Node2[?(@.Node3 == 'value')].Node3bis

Postman 不理解 [?(@.Node3 == 'value')] 语法,因为本机不支持 jsonPath。

因此,我尝试在 Postman 中导入 jsonPath js 库,我找到了两个:


研究


未知域

即使我看了他们的全球使用,我对 NodeJs / NPM / Newman 的技术也一无所知。

但是我读到 postman 支持 NodeJs,允许生成 NodeJs 代码片段,我看到可以使用 NodeJs 导入 jsonPath 库。

也许可以从那里做一些事情,但我没有实现它的专业知识。


问题:

有谁知道如何让它一起工作?

我的意思是,找到一种使用 jsonPath 表达式的方法,感谢 jsonPath js 库,在邮递员中...... 请帮忙。

欢迎任何邮递员 collection 让它工作:)。

关于最初的问题,目前不支持导入外部js代码(除了上面揭示的全局变量技巧),但Postman团队正在开发中。 canary版本可以在正式发货前拿到。

根据我的需要,我实际上找到了一种使用 cheerio 构建带条件路径的方法。 注意:responseBody 在 XML 中,因此我加载了 cheerio。

var $ = cheerio.load(responseBody, {
  ignoreWhitespace: true,
  xmlMode: true
});

//Will show in the console the value of NODE3bis corresponding to a NODE3='value'
console.log("cheerio xpath with condition: " + 
$('NODE1 > NODE2 > NODE3:contains("value")')
    .parent().find('NODE3bis').text());
//Where NODE3 and NODE3bis are sibling. 

//Another way to write it using .has() function, which allows to avoid the use of .parent() afterward:
console.log("cheerio xpath with condition: " + 
$('NODE1 > NODE2').has('NODE3:contains("value")').find('NODE3bis').text());