AWS API Gateway JSON输入的意外结束

时间:2017-10-04 04:53:43

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

我们目前正在切换到AWS API Gateway的公司API。 API网关中的端点使用Node.js Lambda函数来命中我们现有的内部端点,使用AWS进行速率限制和身份验证。我的第一个端点工作得很好,但我的第二个端点给了我一个空白的响应,在CloudWatch中我看到以下错误:

[{"name":"Distinct Zips","offers":8,"affiliates":1,"margin":0,"profit":0,"paid":0,"received":0,"conversion_rate":100,"average_profit":0,"total_calls":1,"qualified_calls":1,"duplicate_calls":0,"returned_calls":0},{"name":"","offers":0,"affiliates":0,"margin":0,"profit":0,"paid":0,"received":0,"conversion_rate":0,"average_profit":0,"total_calls":0,"qualified_calls":0,"duplicate_calls":0,"returned_calls":0},{"name":"Total","offers":8,"affiliates":1,"margin":0,"profit":0,"paid":0,"received":0,"conversion_rate":100,"average_profit":0,"total_calls":1,"qualified_calls":1,"duplicate_calls":0,"returned_calls":0}]

如果我直接点击我们的API,它会正确返回有效的JSON

callback = function(response) {
    var responseString = '';

    // Another chunk of data has been recieved, so append it to `str`
    response.on('data', function (chunk) {
        responseString += chunk;
    });

    // The whole response has been received
    response.on('end', function () {
        console.log(responseString);
        // Parse response to json
        var jsonResponse = JSON.parse(responseString);

        var output = {
            status: response.statusCode,
            bodyJson: jsonResponse,
            headers: response.headers
};

JSON是有效的,所以我不确定为什么AWS会在意外结束时返回错误。我尝试将结果更改为单个JSON项,而不是数组,但在CloudWatch中仍然遇到相同的错误。我甚至不确定从哪里开始查看,如果它可能是我们的Lambda函数的问题,或者它与我们的代码库实际返回的内容有关。

为了清晰起见而编辑

请求使用Lambda函数集成,但不使用Lambda代理集成。完整的lambda可以在https://gist.github.com/awestover89/a53c0f2811c566c902a473ea22e825a5

看到

我们使用回调处理Lambda中的分块数据:

POST

2 个答案:

答案 0 :(得分:0)

问题在于我们的应用程序结束了。 API Gateway为我们的端点设置作为application / json的所有请求的Content-Type标头,并且当content-type设置为JSON时,我们的应用程序会清除所有查询字符串参数,以支持拉取正文。

此端点有一些必需的查询字符串参数被删除,因此失败并且它发回的错误消息没有正确格式化JSON,因此Node无法解析它。

答案 1 :(得分:0)

对于它的价值,我最近遇到了此错误,然后在30分钟后消失了。 API Gateway可能是愚蠢的?

相关问题