【问题标题】:AWS API GATEWAY - empty response bodyAWS API GATEWAY - 空响应正文
【发布时间】:2019-10-25 13:21:47
【问题描述】:

我正在使用 node serverless 和 python 在 AWS 上部署服务。 使用的服务:S3、API-Gateway、Cloudformation、Lambda。

问题是……我得到的是空的响应正文:

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

“正文”字段为空。当我用 POSTMAN 测试时也是一样。 但是当我在 lambda 上测试时,它工作正常:

{
  "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\"}"
}

这里是 yml 文件:

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

【问题讨论】:

    标签: amazon-web-services


    【解决方案1】:

    如果您从 API Gateway 异步调用了 lambda 函数,那么收到空响应是正常的,因为在这种情况下,API Gateway 不会等待 lambda 响应,而只是返回一个空响应。但是,您可以根据状态代码从 API 网关创建自己的集成响应模板。

    如果您已经同步调用,但仍未收到响应,那么您也可以从 API 网关调用您的 lambda 函数,并观察您的 lambda 函数是否正常工作。

    【讨论】:

      猜你喜欢
      • 2018-05-02
      • 2016-04-16
      • 2022-10-14
      • 2018-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 2017-08-08
      相关资源
      最近更新 更多