Visualizing Responses
Apidog offers a programmable approach to visualize response data, allowing you to present response information in an easily understandable manner. This enables team members to gain a clearer understanding of the current interface documentation through visualized responses.
Adding Custom Script
In Apidog, you can add custom scripts in pre-operation or post-operation.
Invocation Method
In the custom script, use the pm.visualizer.set() function to display the visualized result in the Visualize tab of the response body.
Code Example
In the post-operation, you can use a custom script to extract links from the response and generate base64 images.
// Translated to English, keeping the format:
// Encapsulate the API response data into the required structure
var resp = {
response: pm.response.json()
}
// HTML template string
var template = `<html><img src="{{response.data}}" /></html>`;
// Set visualizer data by passing the template and the parsing object.
pm.visualizer.set(template, resp);
##pm.visualizer.set()
This method accepts 3 parameters.
The
template
parameter (required)template
is a required parameter, The first parameter is the HTML template string received by Handlebars , which will eventually be rendered under<body>
.You can write<link>
intemplate
to load external css stylesheets, or use<script>
to load third-party libraries.The
data
parameter (optional)Select the parameter
data
to receive an object that is used to replace the template string variable of Handlebarsconst template = `<div>{{name}}</div>`;
pm.visualizer.set(template, {
name: 'Apidog'
})
// The rendered result is <div>Apidog</div>The
options
parameter (optional)It is the same as the options parameter received by the
options
method. It is used to configure how Handlebars compiles the template string passed in the first parameter.
pm.getData(cb: (err, data) => void)
The parameter received by this method is a function that allows you to get the data of the second parameter you passed in the template string passed in pm.visualizer.set()
.
error
error messageThe data passed in by
data
through the second parameterpm.visualizer.set()
const template = `
<div>{{name}}</div>
<script>
pm.getData(function(err, data){
// Do the corresponding operation according to the incoming data in the callback function
console.log(data.name)
// Apidog
})
</script>
`
pm.visualizer.set(template, {
name: 'Apidog'
})
You cannot call Worker
and indexedDB
in the window
object in the template string.