【问题标题】:Azure Functions JS: http(s) POST request call inside function not workingAzure Functions JS:函数内部的 http(s) POST 请求调用不起作用
【发布时间】:2019-07-16 13:46:48
【问题描述】:

我目前正在从 aws lambda 切换到 azure 函数,并尝试将我的 lambda 函数 (js) 转换为 azure 函数 (js)。我需要在我的函数中做的一件事是将 HTTPS 发布请求发送到 URL 以获取一些数据。这在 aws lambda 中运行良好。但是,似乎 azure 函数要么不支持这一点,要么我做错了,因为它从不发送请求,只是结束了整个函数。

这是我的代码:

var https = require('https');
var http = require('http');


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

        var http_options = {
                hostname: 'somehostname',
                port: 443,
                path: 'somepath',
                method: 'POST',
                headers: {
                        'Content-Type': 'text/xml;charset=UTF-8',
                        'SOAPAction': '"https://someURL"'
                }
        };

        var body = '';

        context.log('before request');

        var req = await https.request(http_options, function (res) {

            res.setEncoding('utf8');
            body = '';

            context.log('inside request');

            res.on('data', (chunk) => {

                    body = body + chunk;
            });

            context.log('in req 2');

            res.on('end', () => {
                    var options = {
                            compact: false,
                            ignoreComment: false,
                            spaces: 1
                    };

                    var result = JSON.parse(body);
            })
        })


};

该函数总是打印“请求前”部分,然后就终止了。

我还尝试了一个简单的 http 调用,如this SO 问题中所述。然而,同样的结果,函数刚刚结束。

【问题讨论】:

    标签: javascript node.js azure https azure-functions


    【解决方案1】:

    我不认为https.request() 是一种异步方法(不返回类似promise 的类型)。如果您尝试在那里删除 await 关键字会怎样?

    var https = require('https');
    var http = require('http');
    
    
    module.exports = async function (context, req) {
    
            var http_options = {
                    hostname: 'somehostname',
                    port: 443,
                    path: 'somepath',
                    method: 'POST',
                    headers: {
                            'Content-Type': 'text/xml;charset=UTF-8',
                            'SOAPAction': '"https://someURL"'
                    }
            };
    
            var body = '';
    
            context.log('before request');
    
            https.request(http_options, function (res) {
    
                res.setEncoding('utf8');
                body = '';
    
                context.log('inside request');
    
                res.on('data', (chunk) => {
    
                        body = body + chunk;
                });
    
                context.log('in req 2');
    
                res.on('end', () => {
                        var options = {
                                compact: false,
                                ignoreComment: false,
                                spaces: 1
                        };
    
                        var result = JSON.parse(body);
                });
            });
    
    
    };
    

    【讨论】:

    • 感谢您的意见。我试过了,但仍然是相同的行为。它在“请求前”打印后结束。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 2015-07-07
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多