Wie kann ich in Node.js eine JSON-Datei von einer URL fetchen?

1 Antwort

Ganz einfach mit dem https Modul:


const https = require('https')

const options = {
  hostname: 'api.mojang.com',
  port: 443,
  path: '/users/profiles/minecraft/Player',
  method: 'GET',
  headers: { 'User-Agent': 'Mozilla/5.0' }
}


function getVal() {
  return new Promise((resolve) => {
    const req = https.request(options, res => {
      res.on('data', d => {
        const data = JSON.parse(d);
        resolve(data);
      });
    });
  req.end()
  });
}


async function handleValue() {
  const data = await getVal();
  console.log(data.name);
  console.log(data.id);
}

handleValue();
Woher ich das weiß:Berufserfahrung – Python, JavaScript, Node.js, SQL
AllesKaese3 
Fragesteller
 23.07.2021, 23:05

Danke :)

0