如何在NodeJS中为aws api网关调用添加身份验证

时间:2017-06-03 20:20:46

标签: node.js amazon-web-services aws-api-gateway

我使用以下选项从节点调用AWS APi Gateway:

var post_options = {
        host: 'myurl.execute-api.us-west-2.amazonaws.com',
        port: '443'
        path: '/staging/api',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Content-Length': Buffer.byteLength(post_data)
        }
    };

通过以下方式进行通话:

http.post(post_options, post_data, function(res){
            res.setEncoding('utf8');
            res.on('data', function(chunk) {
                console.log(chunk);
            });
        });

我得到的回应是:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
</HEAD><BODY>
<H1>ERROR</H1>
<H2>The request could not be satisfied.</H2>
<HR noshade size="1px">
Bad request.
<BR clear="all">
<HR noshade size="1px">
<PRE>
Generated by cloudfront (CloudFront)
Request ID: Yl1rUhK7Bmaq57ckHvK1E32tqNqDs4GC078yek3_23_RMefXUGJAdA==
</PRE>
<ADDRESS>
</ADDRESS>
</BODY></HTML>
name: undefined
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
</HEAD><BODY>
<H1>ERROR</H1>
<H2>The request could not be satisfied.</H2>
<HR noshade size="1px">
Bad request.
<BR clear="all">
<HR noshade size="1px">
<PRE>
Generated by cloudfront (CloudFront)
Request ID: nJ3dotCRum8XudR6v0PinqzDjjPCX3N5TmoM2fNQSJK9BLn8jJEWqg==
</PRE>
<ADDRESS>
</ADDRESS>
</BODY></HTML>

我想,我得到了'#34; Bad request&#34;因为我没有将任何身份验证信息传递给我的电话。 添加此类身份验证信息的正确语法是什么?

1 个答案:

答案 0 :(得分:0)

API网关不支持分块编码,您尝试在后端使用的是什么?如果通过API网关返回的响应标头指示我们应该使用分块编码,则可能存在问题。

您不应该收到此错误,但我想确定有关您的API /后端的更多细节。