【问题标题】:How do I block undefined variables from being sent on Postman?如何阻止未定义的变量在 Postman 上发送?
【发布时间】:2019-09-03 01:36:08
【问题描述】:

我正在使用 Postman 对使用 CSV 文件作为外部文件数据的 API 运行测试。在 CSV 文件中,有多个数据集,数据量不同,有的有数据,有的没有。当我运行 collection runner 并运行第一个数据集时,它会失败,因为有尚未定义的变量并且 API 会引发数据错误。

在 CSV 中

  1. 数据集 1

    • 订购, 客户编号, 条码 1, 数量 1, 单价 1
  2. 数据集 2

    • 订购, 客户编号, 条码 1, 数量 1, 单价 1, 条码 2, 数量 2, 单价2
  3. 数据集 3

    • 订购, 客户编号, 条码 1, 数量 1, 单价 1, 条码 2, 数量 2, 单价 2, 条码 3, 数量 3, 单价 3
In the body I've added that extra variables in case it is available in a data set.

{
  "order"  :  "{{orderId}}",
  "clientId" : "{{clientId}}",
  "skus"  :  [
        {
            "barcode": {{barcode1}},
            "quantity": {{quantity1}},
            "unitPrice": {{price1}}
        },
         {
            "barcode": {{barcode2}},
            "quantity": {{quantity2}},
            "unitPrice": {{price2}}
        },
         {
            "barcode": {{barcode3}},
            "quantity": {{quantity3}},
            "unitPrice": {{price3}}
        }
    ]

}

这是回复:

{
 "order" : "1000305408",
 "clientId" : "30",
 "skus" : [
 {
 "barcode": 123123123,
 "quantity": 1,
 "unitPrice": 100
 },
 {
 "barcode": {{barcode2}},
 "quantity": {{quantity2}},
 "unitPrice": {{price2}}
 }
 ]
} 

有没有办法阻止那些未定义的变量键和数据,同时仍将其保留在正文中,以防万一新数据集具有变量?

【问题讨论】:

  • 这个问题比这里发布的 99.9% 的问题都要好。
  • 哈哈感谢编辑!

标签: javascript json postman


【解决方案1】:

我们可以使用上传的 CSV 文件动态找出存在的集合数。

我只是检查那里有多少条码集并使用它,我正在准备一个setCount,它将用于循环数据并创建skus 项目。

预请求脚本:

_ = require('lodash');

// Here we'll know how many unit counts we have, I am checking barcode only.
let setCount = _.chain(data) // data is the actual iteration data from the csv file, don't worry just run the script.
    .keys()
    .countBy((item) => item.includes('barcode'))
    .value()
    .true,

    skus = [];


for(var i = 1; i <= setCount; i++) {
    skus.push({
        barcode: data[`barcode${i}`],
        quantity: data[`quantity${i}`],
        unitPrice: data[`unitprice${i}`]
    });
}

let requestBody = {
    order: data["orderId"],
    clientId: data["clientId"],
    skus: skus
};

pm.variables.set('requestBody', JSON.stringify(requestBody));

在您的请求正文选项卡中执行以下操作:

使用 JSON 将正文设置为 raw,并在正文中添加 {{requestBody}} 作为变量。

如果有任何混淆,您可以参考屏幕截图。

【讨论】:

  • 我已经尝试过了,但是对于空变量,它仍然会通过,因为它发送 null 并给出数据错误。 {"skus":[{"barcode":5702015989480,"quantity":1},{"barcode":5702015989480,"quantity":1},{"barcode":null,"quantity":null},{"条码":null,"数量":null}]}
  • 好的,我添加了 if else 条形码,只是为了解决空问题,它可以工作! :) for(var i = 1; i &lt;= setCount; i++) { if (data[barcode${i}]){ skus.push({ barcode: data[barcode${i}], quantity: data[quantity${i}], unitPrice: data[unitprice${i}] }); } }
【解决方案2】:

您需要使用 预请求脚本 来动态创建 JSON。

这实际上是需要的:

var body = {
    order : pm.environment.get('orderId'),
    clientId : pm.environment.get('clientId'),
    skus : []
};

for (var i = 1; i <= 3; i++) {
    var barcode = pm.environment.get('barcode' + i);
    if (barcode) {
        var quantity = pm.environment.get('quantity' + i);
        var price = pm.environment.get('price' + i);
        var obj = {
            barcode : barcode,
            quantity : quantity,
            unitPrice : price
        }
        body.skus.push(obj);
    }
}
pm.environment.set('dynamic_json', JSON.stringify(body));

然后在您通常放置固定 JSON 的 Body 选项卡中,只使用这个:

{{dynamic_json}}

【讨论】:

  • 这不适用于运行器和 CSV 文件,但在已经设置环境时可以正常工作
猜你喜欢
  • 2015-04-11
  • 2016-08-25
  • 2014-12-01
  • 2011-06-28
  • 2015-11-19
  • 2021-07-02
  • 1970-01-01
  • 1970-01-01
  • 2019-09-17
相关资源
最近更新 更多