如何通过Azure函数进行https调用?

时间:2018-09-28 00:11:54

标签: node.js azure azure-functions

我正在尝试使用nodeJS创建Azure函数,但是当我调用https API时会收到错误消息。

是否可以通过azure函数进行HTTPS调用?

这是我的代码

const https = require('https');
const querystring = require('querystring');

module.exports = async function (context, req) {

    if (req.query.accessCode || (req.body && req.body.accessCode)) {

        var options = {
            host: 'api.mysite.com',
            port: 443,
            path: '/oauth/access_token',
            method: 'POST'
        };

        var postData = querystring.stringify({
            client_id : '1234',
            client_secret: 'xyz',
            code: req.query.accessCode
        });


        var req = https.request(options, function(res) {
            context.log('STATUS: ' + res.statusCode);
            context.log('HEADERS: ' + JSON.stringify(res.headers));

            res.on('data', function (chunk) {
                context.log('BODY: ' + chunk);
            });
        });

        req.on('error', function(e) {
            context.log('problem with request: ' + e.message);
        });

        req.write(postData);
        req.end();

        context.res = {
            status: 200,
            body: "Hello " + (req.query.accessCode)
        };
    } else {
       context.res = {
            status: 400,
           body: "Please pass a name on the query string or in the request body"
      };
    }

    context.done();
};

我收到一个错误,但是在控制台上看不到任何错误,如果我注释所有的https调用,它也可以正常工作,并且我可以在屏幕上看到Hello消息。

2 个答案:

答案 0 :(得分:3)

有两点要解决

  1. 删除context.done();。参见Azure document

      

    如果您的函数使用JavaScript异步函数声明(在2.x版的Node 8+中可用),则无需使用context.done()。隐式调用context.done回调。

  2. 将您的https.rename重命名为var myReq = https.request(options, function(res)

    由于函数声明了内置的req对象,因此名称冲突引起错误。

答案 1 :(得分:0)

这是可能的,下面是一个如何向Azure AD v2令牌终结点发出请求的示例(我假设您正在尝试执行类似的操作):

var http = require('https');

module.exports = function (context, req) {
    var body = "";
    body += 'grant_type=' + req.query['grant_type'];
    body += '&client_id=' + req.query['client_id'];
    body += '&client_secret=' + req.query['client_secret'];
    body += '&code=' + req.query['code'];

    const options = {
        hostname: 'login.microsoftonline.com',
        port: 443,
        path: '/ZZZ920d8-bc69-4c8b-8e91-11f3a181c2bb/oauth2/v2.0/token',
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': body.length
        }
    }

    var response = '';
    const request = http.request(options, (res) => {
        context.log(`statusCode: ${res.statusCode}`)

        res.on('data', (d) => {
            response += d;
        })

        res.on('end', (d) => {
            context.res = {
                body: response
            }
            context.done();
        })
    })

    request.on('error', (error) => {
        context.log.error(error)
        context.done();
    })

    request.write(body);
    request.end();
};

区别在于-函数不是异步module.exports = function

我相信您的问题是:

您应该使用Node.js实用程序函数util.promisify将错误优先的回调样式函数转换为等待的函数。

link

相关问题