在 PostMan 中更改响应

编程入门 行业动态 更新时间:2024-10-25 06:28:42
本文介绍了在 PostMan 中更改响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用的是开放式 API.

I'm using an open API.

但我只使用了 API 提供的响应中的一小部分数据.当我使用具有不同参数的 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.

我不想在每次发送请求时看到整个 API 响应,我只想看到我感兴趣的数据.

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.

例如:

响应有 3 个对象.Status、Features 和 Data.但是我只对 Data 对象感兴趣,我只想在发出请求时看到 Data 对象

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?

运行测试以验证数据,并且使用 Pre-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.

推荐答案

没有修改 body 的选项,但你可以使用 postman 中惊人的可视化功能:

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

例如:

设置网址和方法:

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

在测试脚本中添加以下代码:

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 });

现在点击可视化:

您可以看到可视化将只显示名字和电子邮件作为表格.

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

你可以在你的情况下使用相同的逻辑

You can use same logic in your case

如果您想将其打印为 json 本身,请在测试脚本中使用以下代码:

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) });

输出:

更多推荐

在 PostMan 中更改响应

本文发布于:2023-10-29 04:07:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538677.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PostMan

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!