最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Lambda isn't returning the body with an http response to API Gateway - Stack Overflow

programmeradmin2浏览0评论

Response shows just fine on Lambda alone when I perform a very simple query with http js module but when I call the function through API Gateway the body is returned empty, even after I put the result on the response body.

response = { 
           "statusCode": 200,
           "body": JSON.stringify(results),
           "isBase64Encoded": false
           };
           callback(null, response, connection.end());

From this I get:

{
"statusCode": 200,
"body": "[]",
"isBase64Encoded": false
}

on API Gateway but it shows as

{
"statusCode": 200,
"body": "[{\"id\":15,\"username\":\"3FWF34fWF\",\"password\":\"sha1$02f44ad2$1$c81a28b3217a4fb777850e85dbdfa088ab44266d\",\"email\":\"3514t34fg\",\"activated\":0,\"name\":\"fEEFEFAE4\"}]",
"isBase64Encoded": false
}

when I test the code on lambda.

Any help would be amazing. I am able to give more info anytime.

Response shows just fine on Lambda alone when I perform a very simple query with http js module but when I call the function through API Gateway the body is returned empty, even after I put the result on the response body.

response = { 
           "statusCode": 200,
           "body": JSON.stringify(results),
           "isBase64Encoded": false
           };
           callback(null, response, connection.end());

From this I get:

{
"statusCode": 200,
"body": "[]",
"isBase64Encoded": false
}

on API Gateway but it shows as

{
"statusCode": 200,
"body": "[{\"id\":15,\"username\":\"3FWF34fWF\",\"password\":\"sha1$02f44ad2$1$c81a28b3217a4fb777850e85dbdfa088ab44266d\",\"email\":\"3514t34fg\",\"activated\":0,\"name\":\"fEEFEFAE4\"}]",
"isBase64Encoded": false
}

when I test the code on lambda.

Any help would be amazing. I am able to give more info anytime.

Share Improve this question edited Feb 8, 2019 at 19:45 Asger asked Feb 8, 2019 at 19:23 AsgerAsger 831 silver badge11 bronze badges 2
  • what type of integration are you doing? have you enabled lambda proxy integration? – asr9 Commented Feb 8, 2019 at 20:47
  • Yes I did @ASR, it's enabled. – Asger Commented Feb 8, 2019 at 20:52
Add a ment  | 

2 Answers 2

Reset to default 3

I managed to fix the issue, the problem was the lambda function not reading the headers I was passing it correctly. Bare in mind that you can access to the event headers in lambda by using event.headers.

If you get this problem double check the event you are receiving on lambda by returning the whole event as a response instead of the actual results such as:

response = { 
       "statusCode": 200,
       "body": JSON.stringify(event),
       "isBase64Encoded": false
       };
       callback(null, response, connection.end());

I have similar issue. I use python Instead.

   body = {
        "statusCode": 200,
        "isBase64Encoded": False,
        "headers": {'Content-Type': 'application/json'},
        # "body": {"recc":response}
        "body":  response
    }

where the response in lambda is:

 "{\"55b7badc-75af-41c0-9877-af308264cb33\": \"0.4666666666666667\", \"4694e172-322e-4a51-930e-d3b9bfd3c2e6\": \"0.36363636363636365\", \"c5447cc5-936d-4aa6-97c4-3f51a7e7c283\": \"0.3\", \"6abf0893-5d32-4a43-942f-aaef4395d91d\": \"0.2727272727272727\", \"c0bf1214-fb41-48eb-b07d-f81b71ba0061\": \"0.25\"}"

Which is correct for api gateway, byt somehow I get:

{
  "statusCode": 200,
  "isBase64Encoded": false,
  "headers": {
    "Content-Type": "application/json"
  },
  "body": "{}"
}
发布评论

评论列表(0)

  1. 暂无评论