简体   繁体   中英

How to make an axios request with the respone of another axios request

// This request is responsible for getting the URL of the page that I will do web scrapping on

axios.post("https://async.scraperapi.com/jobs", {
   apiKey: KEY,
   url: URL,
}).then(data => {
     getThePrice(data.data.statusUrl);
 });

//This request is responsible for doing web scraping for the URL I got from the previous request

function getThePrice(scrapedURL) {
   axios.get(scrapedURL).then(data => console.log(data));
}

The problem is, the second request is being called before the first one has ended so I'm not getting the result I'm expecting I tried to do the 2 requests separately by getting the link first then applying on the second one and it works but now I need to do them together

Not sure if there is problem with the API itself or with network connection but this is how I would do it with async await. It is also helpful to catch the error with try catch statement so you can better understand it.

  const handleRequests = async () => {
try {
  const { data } = await axios.post('https://async.scraperapi.com/jobs', {
    apiKey: KEY,
    url: URL,
  })
  const { data: secondReqData } = await axios.get(data?.statusUrl)
  console.log(secondReqData)
} catch (e) {
  console.log(e)
}

}

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