Avatar ·

How to make an HTTP request in JavaScript?

Hello everyone! Please tell me how to properly make an HTTP request in Node.js (version 18).

I'm writing a small script for parsing data from a public API. I need to send a GET request and get a JSON response. I tried using the built-in http module, but ran into a problem: the data arrives in chunks, and I have to manually collect it using the data and end events. This seems inconvenient and cumbersome.

const http = require('http');\n\nhttp.get('http://example.com/api/data', (res) => {\n  let data = '';\n  res.on('data', (chunk) => { data += chunk; });\n  res.on('end', () => {\n    console.log(JSON.parse(data));\n  });\n});

I've heard that there are libraries like axios or node-fetch, but