在Postman Collection Runner中使用包含JSON对象的模板变量

时间:2019-09-03 22:08:52

标签: json postman postman-collection-runner

我正在尝试使用Postman集合运行器将多个对象从JSON文件中发布到API,该文件的结构如下:

[
    {
        "id": "170",
        "body": {
            "name": "prod1",
            "category": "category1"
        },
        "anotherProperty": "xyz"
    },
    {
        "id": "171",
        "body": {
            "name": "prod3",
            "category": "category1"
        },
        "anotherProperty": "dfg"
    },
    {
        "id": "172",
        "body": {
            "name": "prod3",
            "category": "category1"
        },
        "anotherProperty": "abc"
    }
]

我的问题似乎在于身体,因为它是一个物体:

这是我的收藏夹正在使用的请求的正文>原始应用程序/ json中的内容:

{
    "$id": "{{id}}",
    "body": {{body}},
    "anotherProperty": "{{anotherProperty}}"
}

当查看插入的内容时,它看起来像:

{
    "id": "170",
    "body": {[object Object]}, // instead of the actual object
    "anotherProperty": "xyz"
}

1 个答案:

答案 0 :(得分:1)

我需要在请求前脚本中添加以下内容:

let properties = pm.iterationData.get('properties');
pm.variables.set('properties', JSON.stringify(properties));

let body = pm.iterationData.get('body');
pm.variables.set('body', JSON.stringify(body));

并且原始JSON毫无问题地插入了!