【问题标题】:Importing external libraries in Postman在 Postman 中导入外部库
【发布时间】:2019-07-11 23:25:06
【问题描述】:

我的需要: 我想在我的 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,但没有一个答案能满足我的需求。

  • Postman js 代码仅限于sandbox

  • Postman 将库限制为 this list

  • 有一个trick(检查提示#5)添加js代码作为全局变量,并在请求中调用它。但是它不适用于 jsonPath 项目,因为它不仅仅是一个简单的函数,而是一个完整的项目。我尝试了很多不同的方法,但仍然无法从我的 Postman 测试中调用 jsonPath 函数。

  • 我还阅读了related Postman issue

  • 来自 GitHub 社区,this issue 已关闭以支持 this one。已关闭以支持this one。自 2015 年 6 月以来一直没有明确答案(只是上面提到的小技巧)。


未知域

即使我阅读了它们的全球使用情况,我对 NodeJs / NPM / Newman 的技术一无所知。

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

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


问题:

有谁知道如何让它完全发挥作用?

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

欢迎任何使其工作的邮递员收藏:)。

【问题讨论】:

    标签: javascript node.js json postman jsonpath


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2015-09-18
      • 1970-01-01
      • 2014-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多