Reputation:
I am creating a common Azure function using JavaScript. The function will handle POST calls and POST the values to Another API. The function is working as expected, but i am unable to get the response value after POST method using request in Azure function
module.exports = async function (context, req) {
var request = require("request");
var options = {
url: "",
method: "POST",
json: true,
body: req.body,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
};
return new Promise(async (resolve, reject) => {
var responseData;
var req = await request.post(options, (err, res, body) => {
if (err) {
console.log(err);
reject(err);
} else {
responseData = body;
console.log(responseData); // it works
resolve(responseData);
}
});
});
}
The same logic works when executed as separate node application in local machine. How to get the response value and pass to the origin?
Upvotes: 1
Views: 1169
Reputation: 964
The async/await doesn't do anything. Request module does not return a Promise.
It will better if you remove your redundancy code like
module.exports = function (context, req) {
var request = require("request");
var options = {
url: "",
method: "POST",
json: true,
body: req.body,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
};
return new Promise((resolve, reject) => {
var responseData;
request.post(options, (err, res, body) => {
if (err) {
console.log(err);
reject(err);
} else {
responseData =res.body;
console.log(responseData); // it works
resolve(responseData);
}
});
});
}
Upvotes: 1