Home > Net >  How do you get data from a link using axios?
How do you get data from a link using axios?

Time:02-04

I have the following link that has json data. How do I call it with axios?

https://gist.githubusercontent.com/graffixnyc/ed50954f42c3e620f7c294cf9fe772e8/raw/925e36aa8e3d60fef4b3a9d8a16bae503fe7dd82/lab2

This is how I am trying to call it:

 async function getPeople(){
    const { data } = await axios.get('https://gist.githubusercontent.com/graffixnyc/ed50954f42c3e620f7c294cf9fe772e8/raw/925e36aa8e3d60fef4b3a9d8a16bae503fe7dd82/lab2.json');
    
    return data;    
}

But, this doesn't work because it is not actually a json file and I can't find another way to do it.

CodePudding user response:

axios sends GET request to the passed address, and you get the response file. It's not like properties in OOPL.

CodePudding user response:

Your raw link doesn't need a .json in the end.

This should just work:

async function getPeople(){

    // exact raw URL, no need to append an extra .json extension
    const { data } = await axios.get('https://gist.githubusercontent.com/graffixnyc/ed50954f42c3e620f7c294cf9fe772e8/raw/925e36aa8e3d60fef4b3a9d8a16bae503fe7dd82/lab2');
    
    return data;    
}

CodePudding user response:

You can use like this:

import axios from "axios";
async function getPeople(){
 const httpClient = axios.create({
    baseURL: "https://gist.githubusercontent.com",
    timeout: 60000,
  });

  let { data } = await httpClient.get(
    "/graffixnyc/ed50954f42c3e620f7c294cf9fe772e8/raw/925e36aa8e3d60fef4b3a9d8a16bae503fe7dd82/lab2"
  );
return data;
}
  •  Tags:  
  • Related