Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postman render html response and execute JavaScript

Tags:

postman

I have an http api that give me html response, and I want to "preview" it.

But there is some javascript code in it, and without execute them, it won't give me the right page.

I currently manually copy & paste them in some aaa.html file and use chrome to open it(file://aaa.html), but I want to simplified those steps.

Is there anyway to do that in postman? or is there any postman alternative can do that?

like image 875
user1686407 Avatar asked Nov 22 '19 07:11

user1686407


People also ask

Does Postman execute JavaScript?

The Postman Sandbox is a JavaScript execution environment that's available to you while writing pre-request and test scripts for requests (both in Postman and Newman). Whatever code you write in these sections is executed in this sandbox.

How do I visualize HTML response in Postman?

To visualize your response data, add code to the Pre-request or Tests script for the request. The pm. visualizer. set() method will apply your visualizer code to the data and present it in the Visualize tab when the request runs.

Why I am getting HTML response in Postman?

This means that the api which you are hitting have some issues. So in your case most probably you have some issues with your php laravel code, that's why whenever you are calling that api then html code was returned on behalf of that.

How do you show response in Postman?

In the latest version of Postman you can see all the data from the collection run for each individual request. In the Collection Runner, Click on the request name and all the details of the request and response can be viewed.


Video Answer


2 Answers

There is an alternative to do this in Postman itself. You will have to use pm.visualizer. Just open your API request which is giving you the HTML response. Then go to the Test tab, add the lines below, and then click on the Visualize tab:

// save your html response in the template and then
const template = pm.response.text(); 

// set that template to pm.visualizer
pm.visualizer.set(template);

see in postman

like image 76
Deepak Tiwari Avatar answered Oct 26 '22 17:10

Deepak Tiwari


From Postman official documentation

Postman provides a programmable way to visually represent your request responses. Visualization code added to the Tests for a request will render in the Visualize tab for the response body, alongside the Pretty, Raw, and Preview options.

You need add to the Tests for a request:

    var template = pm.response.text();
    pm.visualizer.set(template);

and see result on the Visualize tab (after Pretty, Raw, and Preview options). Postman result HTML with JS and CSS


To fix the error:

  • Refused to load the image 'file:///C:/some.png' because it violates the following Content Security Policy directive: "img-src http: https: data:".
  • if the content simply does not find (404 Not Found) it along a similar path 'file:///C:/some.file'

need to add the HTML tag to the section of the response body:

    var response = pm.response.text();
    var base = '<base href="https://some.domain/">";
    var template = response.replace('<head>', '<head>' + base);
    pm.visualizer.set(template);

this answer also solves the question in this comment.


For more information:

  • Postman Visualizing responses
  • HTML <base> tag in MDN Web Docs
like image 2
Vlad_Stiff Avatar answered Oct 26 '22 17:10

Vlad_Stiff