繁体   English   中英

AWS API Gateway JSON输入意外结束

[英]AWS API Gateway Unexpected end of JSON input

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

2017-10-04T03:24:46.957Z 925a40ba-a8b3-11e7-be24-8d954fcaf057 
SyntaxError: Unexpected end of JSON input
at Object.parse (native)
at IncomingMessage.<anonymous> (/var/task/index.js:67:37)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)

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

[{"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}]

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

编辑清晰

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

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

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
};

问题出在我们应用程序的末端。 API网关将对我们端点的所有请求的Content-Type标头设置为application / json,并且当content-type设置为JSON时,我们的应用程序会清理所有查询字符串参数,以方便提取正文。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM