gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
can you give a Javascript code to perform a "GET "asynchronous HTTPS request to extract…
- User
can you give a Javascript code to perform a "GET "asynchronous HTTPS request to extract the HTML from a website
- Assistant
Certainly! Below is an example of how you can perform an asynchronous GET HTTPS request in JavaScript to fetch the HTML content from a website using the Fetch API. This example works in modern browsers and in environments like Node.js (with the
node-fetchlibrary):Using Fetch API in Browser (or modern environments)
async function fetchHTML(url) { try { const response = await fetch(url, { method: 'GET', headers: { 'Accept': 'text/html' } }); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } const html = await response.text(); return html; } catch (error) { console.error('Error fetching HTML:', error); } } // Example usage: fetchHTML('https://example.com') .then(html => { console.log(html); // HTML content of the page });