简体   繁体   中英

read data json from a specific url

i want do display the data inside this url on my node stand out but it display none, there is something i do wrong in my code

const extractRefDefaultSchema = async (data) => {
const url = "https://mos.esante.gouv.fr/NOS/TRE_R81-Civilite/FHIR/TRE-R81-Civilite/TRE_R81-Civilite-FHIR.json"
https.get(url,(res) => {
    let body = "";
    res.on("data", (chunk) => {
        body += chunk;
    });
    res.on("end", () => {
        try {
            let json = JSON.parse(body);
            // do something with JSON
        } catch (error) {
            console.error(error.message);
        };
    });
}).on("error", (error) => {
    console.error(error.message);
});
console.log("extractRefDefaultSchema");

};

You can use fetch API for do this:

 fetch(url).then((res)=> res.json()).then((json) => { let dataJson = JSON.parse(json) })

You can achieve the same with axios , it would be much simpler.

Code:

let axios = require("axios")
axios.get("https://mos.esante.gouv.fr/NOS/TRE_R81-Civilite/FHIR/TRE-R81-Civilite/TRE_R81-Civilite-FHIR.json").then((res) => {
    let jsonData = res.data
}) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM