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);
});
}
};
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 !
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)
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.
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)
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 !
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
that's bang on . thanks buddy