【问题标题】:AWS Lambda HTTPS Request - BlueprintAWS Lambda HTTPS 请求 - 蓝图
【发布时间】:2023-03-30 14:10:02
【问题描述】:

我对 AWS LAmbda 有点陌生,正在尝试通过 AWS Lambda 函数向服务器发出 http 请求。我使用了 AWS HTTPS 蓝图代码,并且只对 option 变量进行了修改(参见下面的代码)。

const https = require('https');

let headers = {
    'Authorization': 'ApiKey {key}'
};

let options = {
    url: 'https://myds.io/api/external/patients/events/medications/',
    headers: headers
};

exports.handler = (event, context, callback) => {
    const req = https.request(options, (res) => {
        let body = '';
        console.log('Status:', res.statusCode);
        console.log('Headers:', JSON.stringify(res.headers));
        res.setEncoding('utf8');
        res.on('data', (chunk) => body += chunk);
        res.on('end', () => {
            console.log('Successfully processed HTTPS response');
            // If we know it's JSON, parse it
            if (res.headers['content-type'] === 'application/json') {
                body = JSON.parse(body);
            }
            callback(null, body);
        });
    });
    req.on('error', callback);
    req.write(JSON.stringify(event.data));
    req.end();
};

当我尝试运行此代码时,我收到以下错误。我无法理解,问题出在哪里以及语法有什么问题。请求你帮助我。

只是为了调用这个 Lambda 函数。我正在使用以下测试功能。

以下是执行结果。

Response:
{
  "errorMessage": "connect ECONNREFUSED 127.0.0.1:443",
  "errorType": "Error",
  "stackTrace": [
    "Object._errnoException (util.js:1022:11)",
    "_exceptionWithHostPort (util.js:1044:20)",
    "TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)"
  ]
}

Request ID:
"0fae1df6-6351-11e8-9741-a3dc748f359b"

Function Logs:
START RequestId: 0fae1df6-6351-11e8-9741-a3dc748f359b Version: $LATEST
2018-05-29T15:00:44.205Z    0fae1df6-6351-11e8-9741-a3dc748f359b    {"errorMessage":"connect ECONNREFUSED 127.0.0.1:443","errorType":"Error","stackTrace":["Object._errnoException (util.js:1022:11)","_exceptionWithHostPort (util.js:1044:20)","TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)"]}
END RequestId: 0fae1df6-6351-11e8-9741-a3dc748f359b
REPORT RequestId: 0fae1df6-6351-11e8-9741-a3dc748f359b  Duration: 35.27 ms  Billed Duration: 100 ms     Memory Size: 128 MB Max Memory Used: 22 MB  

【问题讨论】:

    标签: node.js https aws-lambda


    【解决方案1】:

    问题现已解决。我对语法进行了一些编辑,现在可以正常工作了。

    'use strict'
    
    const https = require('https');
    
    let options = {
        host : 'myds.io',
        path:  '/api/external/patients/events/medications/',
        headers: {
            'Authorization': 'ApiKey *******'
            },
    };
    
    exports.handler = (event, context, callback) => {
        const req = https.request(options, (res) => {
            let body = '';
            console.log('Status:', res.statusCode);
            console.log('Headers:', JSON.stringify(res.headers));
            res.setEncoding('utf8');
            res.on('data', (chunk) => body += chunk);
            res.on('end', () => {
                console.log('Successfully processed HTTPS response');
                // If we know it's JSON, parse it
                if (res.headers['content-type'] === 'application/json') {
                    body = JSON.parse(body);
                }
                callback(null, body);
            });
        });
        req.on('error', callback);
        req.write(JSON.stringify(event.data));
        req.end();
    };
    

    【讨论】:

      【解决方案2】:

      req.write(JSON.stringify(event.data)); 中搜索event.data,但您的测试事件没有data 键,而只有key1, key2, key3

      尝试将您的测试事件更改为类似于以下内容:

      {
         "data": "something"
      }
      

      【讨论】:

      • 我已根据您的建议对问题进行了编辑。但问题还没有解决。想法?
      • 我已经发布了有效的代码。非常感谢您的宝贵时间和建议!
      猜你喜欢
      • 2021-08-10
      • 2018-06-04
      • 1970-01-01
      • 2019-11-15
      • 2020-06-08
      • 2015-10-27
      • 2018-05-04
      • 2021-07-23
      • 2021-12-31
      相关资源
      最近更新 更多