Reputation: 2486
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.
Upvotes: 5
Views: 7870
Reputation: 19989
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:
Upvotes: 9