【问题标题】:How to make https call from Azure function?如何从 Azure 函数进行 https 调用?
【发布时间】:2018-09-28 00:11:54
【问题描述】:

我正在尝试使用 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 消息。

【问题讨论】:

    标签: node.js azure azure-functions


    【解决方案1】:

    两点修复

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

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

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

      由于函数声明了一个内置的req 对象,因此存在名称冲突导致错误。

    【讨论】:

    【解决方案2】:

    这是可能的,这是一个如何向 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-20
      • 1970-01-01
      • 2017-08-31
      • 2016-03-05
      • 2014-03-17
      • 2017-04-21
      • 2021-04-24
      • 2020-04-02
      相关资源
      最近更新 更多