0

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?

1
  • So you want to make an AJAX request to the URL and read the response? Commented Sep 20, 2022 at 13:51

2 Answers 2

0

You can use:

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

const html = await res.text()

Sign up to request clarification or add additional context in comments.

2 Comments

I am getting error that res.text is not a function
Your GET request has no content and therefore does not require a content-type header. You're also missing the URL scheme (http:// or https://)
0

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.