简体   繁体   English

AWS API Gateway JSON输入意外结束

[英]AWS API Gateway Unexpected end of JSON input

We have a company API that we are currently switching over to AWS API Gateway. 我们有一个公司API,目前正在切换到AWS API Gateway。 The endpoints in API Gateway use a Node.js Lambda function to hit our existing internal endpoint, using AWS for rate limiting and authentication. API Gateway中的端点使用Node.js Lambda函数来访问我们现有的内部端点,并使用AWS进行速率限制和身份验证。 My first endpoint worked perfectly, but my second endpoint is giving me a blank response and in CloudWatch I see the following error: 我的第一个端点运行正常,但是我的第二个端点给了我空白的响应,在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)

If I hit our API directly it properly returns valid JSON 如果我直接点击我们的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}]

The JSON is valid, so I'm not sure why AWS is returning an error with unexpected end. JSON是有效的,所以我不确定为什么AWS会返回错误且意外结束的错误。 I tried changing the result to be just a single JSON item, not an array, but still got the same error in CloudWatch. 我尝试将结果更改为单个JSON项,而不是数组,但在CloudWatch中仍然遇到相同的错误。 I'm not even sure where to begin looking, if it's likely an issue with our Lambda function, or if it's something with what our codebase is actually returning. 我什至不确定我们的Lambda函数是否有问题,或者与我们的代码库实际返回的内容有关,该从哪里开始寻找。

Edits for clarity 编辑清晰

The request uses a Lambda function integration but does not use Lambda Proxy integration. 该请求使用Lambda函数集成,但不使用Lambda代理集成。 The full lambda can be seen at https://gist.github.com/awestover89/a53c0f2811c566c902a473ea22e825a5 完整的lambda可以在https://gist.github.com/awestover89/a53c0f2811c566c902a473ea22e825a5中看到

We handle chunked data in the Lambda using the callback: 我们使用回调处理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
};

Problem was on our application's end. 问题出在我们应用程序的末端。 API Gateway sets the Content-Type header for all requests to our endpoints as application/json, and our application scrubs all query string parameters when the content-type is set to JSON, in favor of pulling the body. API网关将对我们端点的所有请求的Content-Type标头设置为application / json,并且当content-type设置为JSON时,我们的应用程序会清理所有查询字符串参数,以方便提取正文。

This endpoint had some required query string parameters that were getting removed, so it failed and the error message it sent back wasn't properly formatted JSON, hence the inability for Node to parse it. 该端点具有一些必需的查询字符串参数,这些参数已被删除,因此它失败了,并且它发送回的错误消息的格式不正确,因此,Node无法解析它。

For what it's worth, I recently had this error then 30 minutes later it went away. 对于它的价值,我最近有此错误,然后30分钟后它消失了。 API Gateway goof perhaps? API Gateway可能是愚蠢的?

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

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