DEV Community

Cover image for axios HTTP request
Yamen Edel
Yamen Edel

Posted on

axios HTTP request

Hi,

I am making an HTTP request through an azure function to invoke a web service in node.js with axios.

This is what this request looks like :

module.exports = async function () {
  const axios = require("axios");
  const data = {
    appUser: "YAMENEDEL",
  };
  const headers = {
    Authorization:
      "Basic WUFNFEWWWRQEQ......",
  };

  {
    axios
      .post(
        "https://tegosGetPutawaysByAppUser?company=grundon",
        data,
        { headers: headers }
      )
      .then((response) => {
        //return (response.data);
        console.log(`Status: ${response.status}`);
        console.log("data: ", response.data);
      })

      .catch((err) => {
        console.error(err);
      });
  }
};
Enter fullscreen mode Exit fullscreen mode

when to test this request on postman it will return 200 response and will see the data in my VS code terminal screen but not in the body response of the client. I tried to return data this way but didn't work return (response.data);

As you can see below a snippet of the postman request with an empty body response !
Image description

Secondly, in the body for this request I am hardcoding the value of the appUser. However, if I want to run this request on postman and pass the JSON value in the body for appUser - what changes do I need to do in the code so the param value can pick up what is being passed. I tried the following appUser: { type: String, default: null } but it was a failed attempt !

Top comments (4)

Collapse
 
thedevcaptain profile image
Francesco Borrelli

so first of all since that is an async function you can await for the response in the function itself and then return the response from axios to the user.

module.exports = async function () {
  const axios = require("axios");
  const data = {
    appUser: "YAMENEDEL",
  };
  const headers = {
    Authorization:
      "Basic WUFNFEWWWRQEQ......",
  };

  {
try{
    const response = await axios.post(
        "https://tegosGetPutawaysByAppUser?company=grundon",
        data,
        { headers: headers }
      );
return response.data;
}catch(e){
// maybe return the error
 console.error(e)}
  }
};
Enter fullscreen mode Exit fullscreen mode

that's because if you return inside the promise 'then', it won't only continue the promise chain, without returning the calling function.
if you want to still use the then.catch chain you can simply return the whole axios.post (so that you are actually returning something)

Collapse
 
yamenad4 profile image
Yamen Edel

Hi Francesco, thanks for the help.

you're right I should use await there. However, the data still not showing on the body response for some reason !

Collapse
 
thedevcaptain profile image
Francesco Borrelli

Okay so I went on the azure documentation and apparently you need to use the context object to return the response to the http request

Like this

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    const name = (req.query.name || (req.body && req.body.name));
    const responseMessage = name
        ? "Hello, " + name + ". This HTTP triggered function executed successfully."
        : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";

    context.res = {
        // status: 200, /* Defaults to 200 */
        body: responseMessage
    };
}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
yamenad4 profile image
Yamen Edel

that's bang on . thanks buddy