在 Postman 中将多级响应对象可视化为表可视、中将、对象、Postman

2023-09-07 10:52:53 作者:该玩的年纪却动了心

我想将 Postman 测试中的以下数据可视化为一个表格,其中列中有 productpricequantity行中的项目.可能有多个 shippingGroups.

I would like to visualize the data below in Postman Tests as a Table with product, price and quantity in columns and Items in rows. There might be multiple shippingGroups.

{
   ...
   "companyGroups": [
        {
            ...
            "shippingGroups": [
                {
                    "id": 1,
                    "items": [
                        {
                            "product": "Product A",
                            "price": 2,
                            "quantity": 1,
                        },
                          {
                            "product": "Product B",
                            "price": 4,
                            "quantity": 4,
                        }

                    ],
                    ...
            ]
        }
    ],

我在使用 {{#each response???}} 引用多个级别对象中的项目时遇到问题.预期的格式应该是这样的:

I have trouble using the {{#each response???}} referring to items within multiple level objects. Expected format should be something like :

   <table>
        <tr>
            <th>Product</th>
            <th>Price</th>
            <th>Quantity</th>
        </tr>

        {{#each response???}}
            <tr>
                <td>{{???product}}</td>
                <td>{{???price}}</td>
                <td>{{???quantity}}
            </tr>
        {{/each}}
    </table>

有关邮递员表可视化响应的更多信息此处

More info on Postman Table Visualizing Response here

推荐答案

鉴于您的响应示例,您可以使用如下内容:

Given your response example, you could use something like this:

const template = `
   <table>
        <tr>
            <th>Product</th>
            <th>Price</th>
            <th>Quantity</th>
        </tr>

        {{#each responseData}}
        {{#each items}}
            <tr>
                <td>{{product}}</td>
                <td>{{price}}</td>
                <td>{{quantity}}
            </tr>
        {{/each}}
        {{/each}}
    </table>
`;

let responseData = []

_.each(pm.response.json().companyGroups, (item) => {
    _.each(item.shippingGroups, (nestedItem) => {
        responseData.push(nestedItem)
    })
})

pm.visualizer.set(template, { responseData })

这只是一个粗略的示例,需要重构,但它表明您可以在表格中显示响应数据.

This is just a rough example and would need to be refactored but it shows that you can show the response data in the table.

 
精彩推荐
图片推荐