Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS API GATEWAY - empty response body

I'am using node serverless and python to deploy service on AWS. Services used: S3, API-Gateway, Cloudformation, Lambda.

The problem is... I am getting empty response body:

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

The field "body" is empty. It is same when I test with POSTMAN. but when I test on lambda it works fine:

{
  "statusCode": 200,
  "isBase64Encoded": false,
  "headers": {
    "Content-Type": "application/json"
  },
  "body": "{\"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\"}"
}

Here is the yml file:

service: collaborative

provider:
  name: aws
  runtime: python3.6
  region: eu-west-1

defaults:
 stage: dev1
 region: eu-west-1

package:
  include:
    - collaborative
  exclude:
    - .git
    - .idea
    - .col_ser.txt

custom:
  integration: lambda

functions:
  collaborative:
    name: lambda-collaborative
    handler: handler.lambda_handler
    events:
      - http:
          path: recommend_user
          method: post
          integration: lambda
          cors: true
          request:
            template:
              text/xhtml: '{ "stage" : "$context.stage" }'
              application/json: '{ "httpMethod" : "$context.httpMethod" }'
          response:
            headers:
              Access-Control-Allow-Origin: "'*'"
            statusCodes:
              400:
                pattern: '.*wrong.*'
                template:
                  application/json: >
                    #set ($errorMessageObj = $input.path('$.errorMessage'))
                    $errorMessageObj

Resources:
  ApiGatewayMethodRecommenduserPost:
    Type: AWS::ApiGateway::Method
    Properties:
      Integration:
        IntegrationHttpMethod: POST
        Type: lambda
like image 374
JohnSmith Avatar asked Jun 11 '19 07:06

JohnSmith


People also ask

How do I troubleshoot HTTP errors in AWS API gateway?

AWS recommends using CloudWatch Logs to troubleshoot these types of errors. In API Gateway, the various HTTP responses supported by your method are represented by method responses. These define an HTTP status code as well as a model schema for the expected shape of the payload for the response.

What is the default response type for the API gateway?

For instructional reasons, the API Gateway console sets the 200 response as the default. But you can reset it to the 500 response. To set up a method response, you must have created the method request. The status code of a method response defines a type of response.

What is the last step in the API gateway response flow?

Note: This section does not apply if you are using the Lambda proxy or HTTP proxy integration. We’re so close. The last step in the API Gateway response flow is to creating your method responses. Method responses are similar to method requests in that they are responsible for validating and standardization.

What is AWS API gateway?

A Detailed Overview of AWS API Gateway AWS API Gateway is an awesome service to use as an HTTP frontend. You can use it for building serverless applications, for integrating with legacy applications, or for proxying HTTP requests directly to other AWS services. But understanding the elements of API Gateway can be difficult.


1 Answers

If you have invoked your lambda function asynchronously from API Gateway then it's normal to receive an empty response because in this case, API Gateway does not wait for lambda to response back and simply return an empty response. However, you can create your own integration response template from API gateway depending upon the status code.

If you have invoked synchronously and still haven't received the response then you can also invoke your lambda function from API gateway and observe if your lambda function is working properly or not.

like image 191
John Avatar answered Oct 01 '22 16:10

John