最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

在Postman中导入外部库

运维笔记admin10浏览0评论

在Postman中导入外部库

在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库,我发现了两个:


研究

  • 我已经阅读了this post,但没有一个答案满足我的需求。
  • 邮差js代码仅限于sandbox。
  • 邮递员将图书馆限制为this list。
  • 有一个trick(检查提示#5)将js代码添加为全局变量,并在请求中调用它。但它不适用于jsonPath项目,因为它不仅仅是一个简单的函数,而是一个整个项目。我尝试了很多不同的方法,但仍然无法通过Postman Test调用jsonPath函数。
  • 我也读过related Postman issue。
  • 来自GitHub社区,this issue被关闭,有利于this one。哪个已被关闭,有利于this one。自2015年6月以来,这个开放时没有明确答案(上面提到的小技巧)。

未知的域名

即使我阅读了它们的全球用途,我也不了解任何关于NodeJs / NPM / Newman的技术。

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

也许可以从那里做点什么,但我没有专业知识来实现​​它。


题:

有谁知道如何使它完全工作?

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

任何使它工作的邮差收藏是受欢迎的:)。

回答如下:

关于最初的问题,目前不支持导入外部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());

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论