当我尝试以编程方式向API发送请求时,我收到了RequestError。我能用邮递员手动完成这个请求就好了,但是当我尝试用Javascript做这件事时它给了我套接字挂断错误。我对STS的调用也正常,我正在获取一套临时凭证。这是test.com
向我发出错误的POST请求。
'use strict'
const AWS = require('aws-sdk');
const config = require('../config');
const rp = require('request-promise-native');
const aws4 = require('aws4');
const Log = require('../logging/logger');
AWS.config = new AWS.Config();
AWS.Config.correctClockSkew = true;
function getTempCredentials() {
const sts = new AWS.STS(
{
apiVersion: '2011-06-15',
accessKeyId: config.awsCreds.accessKeyId,
secretAccessKey: config.awsCreds.secretKey,
region: 'us-east-1'
}
);
const params = {
ExternalId: config.awsCreds.externalId,
RoleArn: config.awsCreds.roleARN,
RoleSessionName: config.awsCreds.roleName
};
return new Promise(function(resolve, reject) {
sts.assumeRole(params, function(error, data) {
if (error) {
Log.error(`Failed to get temporary credentials from AWS. Error: ${JSON.stringify(error)}`); // an error occurred
reject(error);
throw error;
}
else {
resolve(data);
}
});
});
}
async function preRegister(load) {
let tempCreds;
try {
tempCreds = await getTempCredentials();
let options = {
service: 'execute-api',
region: 'us-east-1'
}
let signedRequest = aws4.sign(options, {
accessKeyId: tempCreds.Credentials.AccessKeyId,
secretAccessKey: tempCreds.Credentials.SecretAccessKey,
sessionToken: tempCreds.Credentials.SessionToken
});
let response = await rp({
method: 'POST',
url: 'https://test.com',
headers: signedRequest.headers,
body: load
});
//return response;
} catch (error) {
Log.error(`Failed to pre-register. Error: ${JSON.stringify(error)}`);
throw error;
}
}
这是确切的错误:
error: Failed to pre-register. Error: {"name":"RequestError","message":"Error: socket hang up","cause":{"code":"ECONNRESET","path":null,"host":"test.com","port":443}
我还尝试使用Postman中的相同值向我的请求添加content-type
和content-length
标头,但这也不起作用。我的想法已经不多了。