Skip to content

Instantly share code, notes, and snippets.

@da8865892q
Created April 29, 2020 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save da8865892q/b9c05ab541110cd6d475dcd8b56d35a7 to your computer and use it in GitHub Desktop.
Save da8865892q/b9c05ab541110cd6d475dcd8b56d35a7 to your computer and use it in GitHub Desktop.
Postman use JSON path filter respond content should add code in "Tests" tab.
let template = `
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/jsonpath@1.0.2/jsonpath.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js"></script>
</head>
<body>
<div>
<div>
<input id="filter" style="width:450px;" type="text" placeholder="Example query: $..name.first">
</div>
<div>
<button id="resetButton" style="background-color:red;color:white;">Reset</button>
<input id="showErrors" type="checkbox" value="1"/>
<span class="item-text" style="font-family:courier;">Show Evaluation Errors</span>
</div>
<div id="errors" style="font-family:courier;color:red;display:none;"></div>
<div>
<p id="content" style="font-family:courier;color:green;font-size:18px;"></p>
</div>
</div>
</body>
</html>
<script>
pm.getData( (error, value) => {
const extractedData = jsonpath.query(value, '$');
$(function() {
$('#filter').keyup(function() {
try {
let filteredData = jsonpath.query(extractedData, $(this).val());
$("#content, #errors").empty();
$("#content").append("<pre><code>" + JSON.stringify(filteredData, null, 4) + "</code></pre>");
} catch (err) {
console.info(err);
$("#errors").empty();
$("#errors").append("<pre><code>" + err + "</code></pre>");
}
});
});
$( "#resetButton" ).click(function() {
$("#content, #errors").empty();
$("#filter").val('');
$("#content").append("<pre><code>" + JSON.stringify(extractedData, null, 4) + "</code></pre>");
})
})
$(function() {
$("#showErrors").on("click",function() {
$("#errors").toggle(this.checked);
});
});
</script>`
pm.visualizer.set(template, pm.response.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment