How to convert URL into HTML in JavaScript?

Viewed 49

I want a HTML document of an URL

I have URL like www.example.com and I want the HTML block of the page of URL. How can I achieve this in JavaScript I read that Java has method like IOUtils.toString to do the same. can somebody suggest me how to do this or what is the method in JavaScript as in Java?

2 Answers

You can use:

const res = await fetch("www.example.com", {
  headers:{
    "Content-Type":"text/html"
  }
});

const html = await res.text()

I did it using @Augustine Madu's answer I used axios to call url

        const res = await axios({
            method: 'get',
            url: url,
            headers:{
                "Content-Type":"text/html"
            },
        })
        console.log(res.data)
Related