Wie kann ich in Node.js eine JSON-Datei von einer URL fetchen?
Hallo, allerseits.
In einem Projekt will ich Die UUID eines Minecraft-Spielers herausfinden. dazu gehe ich auf https://api.mojang.com/users/profiles/minecraft/Player Name. Das ist eine JSON Datei. Diese möchte ich jetzt als String in JS haben, damit ich das ID-Element (=UUID) weiterverwenden kann. Ich habe schon einiges recherchiert, aber nichts gefunden. Hat jemand eine Idee, wie ich universell Json Dateien runterladen und als String speichern kann?
Vielen Dank im Vorraus.
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