AWS lambdaからステージング環境にGETリクエストを送信したかったのですが、ベーシック認証がかかっていて、なかなか苦労しました。。。
以下で動いてくれましたので、お試しくださいね!
環境
ランタイム | ハンドラ |
---|---|
Node.js 8.10 | index.handler |
コード
node.js
exports.handler = function index(event, context, callback) {
const util = require('util');
const http = require('https');
const host = '****URL****'; // e.g. test.com
const path = '****/TO/TARGET****'; // e.g. /user/dashboard
const user = '****USERNAME****';
const pwd = '****PASSWORD****';
const req = http.get(
{
"host" : host,
"path" : path,
"auth" : user + ":" + pwd,
},
function(res) {
console.log("[info] Status Code: " + res.statusCode);
console.log("[info] res:" + util.inspect(res, false, null));
},
);
req.on('error', function(e){
console.log("[error] " + util.inspect(e, false, null));
});
};