Saeesh Tendulkar
Saeesh Tendulkar

Reputation: 703

Make consecutive axios post API calls with proper error handling

I need to write an API which will call one POST API and pass the data received from it to another POST API and then return the data from second API to the user.

There has to be also proper error handling done so that the API should return error message and not fail when one of the API's does not work

The code that I have tried is as follows

listPopularPost: async(req, res) => {
    let requestUrl = process.env.USER_SERVICE_URL+"/api/"+process.env.API_VERSION+'/story-popular';
    try{
        await axios.post(requestUrl, req.body, requestHeader).then((response) => {
            if(response.data.success == true){
                let cmsRequestUrl = process.env.CMS_SERVICE_URL+process.env.API_VERSION+"/story/popular";
                let requestParam = {
                    "request" : req.body,
                    "user_params": response.data.story_ids,
                }
                axios.post(cmsRequestUrl, requestParam,requestHeader).then((response) => {
                    res.status(200).send(response.data);
                }, (error) => {
                    res.status(200).send({'success':false,'code':'request_error','message':'error encountered','data':{'error':error}});
                });
            }
        }, (error) => {
            res.status(200).send(error);
        });
    }
    catch(err){
        console.log(err)
        res.status(500).send(err);
    } 
},

But it timesout and does not work. How can I fix this code?

Upvotes: 0

Views: 128

Answers (1)

hyper_debugger
hyper_debugger

Reputation: 69

Since you're using async/await. You could refactor as

listPopularPost: async (req, res) => {
  let requestUrl = process.env.USER_SERVICE_URL + "/api/" + process.env.API_VERSION + '/story-popular';
  try {
    let data = await axios.post(requestUrl, req.body, requestHeader).then((response) => response.data);
    if (data.success) {
      let cmsRequestUrl = process.env.CMS_SERVICE_URL + process.env.API_VERSION + "/story/popular";
      let requestParam = {
        "request": req.body,
        "user_params": data.story_ids,
      }
      data = await axios.post(cmsRequestUrl, requestParam, requestHeader).then((response) => response.data);
      return res.status(200).send(data);
    }

    return res.status(500).send({ 'success': false, 'code': 'request_error', 'message': 'error encountered', 'data': { 'error': new Error("Request wasn't successful") } });
  }
  catch (err) {
    console.log(err)
    res.status(500).send({ 'success': false, 'code': 'request_error', 'message': 'error encountered', 'data': { 'error': error } });
  }
}

or without using async/ await

listPopularPost: (req, res) => {
  let requestUrl = process.env.USER_SERVICE_URL + "/api/" + process.env.API_VERSION + '/story-popular';

  axios.post(requestUrl, req.body, requestHeader).then((response) => response.data)
    .then((data) => {
      if (!data.success) throw new Error("Request was unsuccessful");

      let cmsRequestUrl = process.env.CMS_SERVICE_URL + process.env.API_VERSION + "/story/popular";
      let requestParam = {
        "request": req.body,
        "user_params": data.story_ids,
      }
      return axios.post(cmsRequestUrl, requestParam, requestHeader);
    })
    .then(response => response.data)
    .then(data => res.status(200).send(data))
    .catch(err => {
      console.log(err)
      res.status(500).send({ 'success': false, 'code': 'request_error', 'message': 'error encountered', 'data': { 'error': error } });
    });
}

Upvotes: 1

Related Questions