我们有一个公司API,目前正在切换到AWS API网关。API网关中的端点使用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代理集成。可以在https://gist.github.com/awestover89/a53c0f2811c566c902a473ea22e825a5
上看到完整的lambda。我们使用回调在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网关将所有请求的内容类型标头设置为我们的端点,以应用程序/JSON,以及当content-type设置为JSON时,我们的应用程序磨砂膏所有查询字符串参数都有支持拉动身体。
。此端点有一些所需的查询字符串参数,这些参数被删除了,因此它失败了,并且它发回的错误消息未正确格式化JSON,因此节点无法解析它。
对于它的价值,我最近遇到了这个错误,然后30分钟后它消失了。API网关也许?