更改 PostMan 中的响应 [英] Alter response in PostMan

查看:30
本文介绍了更改 PostMan 中的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm using an open API.

But I am only using a small bit of data from the response the API provides. And as I'm testing using the API with different parameters to see the responses.

I don't want to see the entire API response every time I send the request, I only want to see the data that I'm interested in.

For example :

The response has 3 objects. Status, Features and Data. But I'm only interested in the Data object, I only want to see the Data object when making the request

Is there a way I can print a different Response, using the actual response of the Request?

Tests are run to validate data, and Pre-Request scripts are used to do something before the request, but I haven't found anything that changes the form of the Response.

解决方案

There is no option to modify body but you can use the amazing visualizer feature in postman:

eg:

Set url and method:

    GET :  https://reqres.in/api/users?page=2

Add below code in test script:

template = `<table bgcolor="#FFFFFF">
        <tr>
            <th>Name</th>
            <th>Email</th>
        </tr>

        {{#each response}}
            <tr>
                <td>{{first_name}}</td>
                <td>{{email}}</td>
            </tr>
        {{/each}}
    </table>
`;

// Set visualizer
pm.visualizer.set(template, {
    // Pass the response body parsed as JSON as `data`
    response: pm.response.json().data
});

Now click visualize:

You can see the visualize will show only first_name and email as a table .

You can use same logic in your case

If you want to print it as json itself then use below code in test script:

template = `
<pre><code>{{response}}</code></pre>
`;

console.log( JSON.stringify(pm.response.json().data, undefined, 2))
// Set visualizer
pm.visualizer.set(template, {
    // Pass the response body parsed as JSON as `data`
    response: JSON.stringify(pm.response.json().data, undefined, 2)
});

Output:

这篇关于更改 PostMan 中的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆