kal*_*hua 1 node.js aws-lambda
我试图从Lambda向另一个API发出请求。我发现使用NodeJS http和https模块可以执行GET请求,但是其他任何模块(例如POST)都无法使用;巧合的是,POST是我要尝试调用的服务所需的唯一方法。
这是Lambda执行GET并收到200响应的工作示例:
const https = require('https')
function handler(event, context, callback) {
const options = {
hostname: 'encrypted.google.com'
}
https
.get(options, (res) => {
console.log('statusCode:', res.statusCode);
res.on('end', callback.bind(null, null))
})
.on('error', callback);
}
exports.handler = handlerRun Code Online (Sandbox Code Playgroud)
这样就证明他的要求是允许的。但是,如果脚本尝试使用.request()https(或https)lib / module的方法发出相同的请求,则该请求将永远不会完成,并且Lambda会超时。
const https = require('https')
function handler(event, context, callback) {
const options = {
hostname: 'encrypted.google.com',
method: 'GET'
}
https
.request(options, (res) => {
console.log('statusCode:', res.statusCode);
res.on('end', callback.bind(null, null))
})
.on('error', callback);
}
exports.handler = handlerRun Code Online (Sandbox Code Playgroud)
我不知道我在做什么错。调用https.request()将以静默方式失败-不会引发错误-并且日志中未报告任何内容。
问题是我从来没有用完成请求req.end()。
const https = require('https')
function handler(event, context, callback) {
const options = {
hostname: 'encrypted.google.com',
method: 'GET'
}
https
.request(options, (res) => {
console.log('statusCode:', res.statusCode);
res.on('end', callback.bind(null, null))
})
.on('error', callback)
.end(); // <--- The important missing piece!
}
exports.handler = handlerRun Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2456 次 |
| 最近记录: |