awestover89
awestover89

Reputation: 1763

AWS API Gateway Unexpected end of JSON input

We have a company API that we are currently switching over to 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. My first endpoint worked perfectly, but my second endpoint is giving me a blank response and in CloudWatch I see the following error:

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

[{"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. I tried changing the result to be just a single JSON item, not an array, but still got the same error in 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.

Edits for clarity

The request uses a Lambda function integration but does not use Lambda Proxy integration. The full lambda can be seen at https://gist.github.com/awestover89/a53c0f2811c566c902a473ea22e825a5

We handle chunked data in the Lambda using the callback:

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

Upvotes: 3

Views: 5815

Answers (2)

CamHart
CamHart

Reputation: 4335

For what it's worth, I recently had this error then 30 minutes later it went away. API Gateway goof perhaps?

Upvotes: 1

awestover89
awestover89

Reputation: 1763

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.

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.

Upvotes: 1

Related Questions