我创建了以下代码,从 S3 存储桶中获取
seralizedXmlFile
对象并将其推送到 API 服务。这将返回 FAIL,日志显示
Error: getaddrinfo ENOTFOUND http://url
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
代码:
const AWS = require('aws-sdk');
const https = require('http');
var s3 = new AWS.S3();
var un;
var pw;
var seralizedXmlFile;
let index = function index(event, context, callback) {
//for testing I have named the bucket and key
var params = {
Bucket: "bucket", //event.bucketName,
Key: "personnelData_50112404_635705766849654385.xml" //event.fileName
};
s3.getObject(params, function(data, err)
{
if (data)
{
let seralizedXmlFile = err.Body.toString('utf-8'); // Use the encoding necessary
console.log("objectData " + seralizedXmlFile);
}
});
var ssm = new AWS.SSM({region: 'ap-southeast-2'});
var paramsx = {
'Names' : ['/App/ServiceUsername', '/App/ServicePassword'],
'WithDecryption' : true
};
ssm.getParameters(paramsx, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {console.log('data: ' + JSON.stringify(data)); // successful response
console.log('password: ' + data.Parameters[0].Value);
console.log('username: ' + data.Parameters[1].Value);
pw = data.Parameters[0].Value;
un = data.Parameters[1].Value;
}
const req = https.request('http:/url/api/SyncPersonnelViaAwsApi/Get/5', (res) => {
res.headers + 'Authorization: Basic ' + un + ':' + pw;
let body = seralizedXmlFile;
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');
console.log('returned res: ' + res);
callback(null, res);
});
});
req.end();
});
};
exports.handler = index;
我按照我在发出 HTTPs 请求时发现的 “Error: getaddrinfo ENOTFOUND”错误
上发现的 Q 和 A 进行操作并将代码更改为
var params = {
host: "http://URL",
path: "/api/SyncPersonnelViaAwsApi/Get/5"
};
var req = https.request(params, function(res) {
let data = '';
console.log('STATUS: ' + res.statusCode);
res.setEncoding('utf8');
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
console.log("DONE");
console.log(JSON.parse(data));
但再次引发相同的错误......有人知道问题是什么吗?
我还通过 POSTMAN 测试了 Web 服务 api,所以我可以确认它正在工作
谢谢你
尝试
host: "URL",
而不是
host: "http://URL",
此外,您正在使用
https
库,并且您的 URL 前缀是 http://
,但我认为您可以/应该完全忽略它。
错误:getaddrinfo ENOTFOUND http://url
意味着客户端无法连接到给定的地址。请尝试指定不带 http 的主机:
var params = {
host: "URL",
path: "/api/SyncPersonnelViaAwsApi/Get/5"
};