aws api 网关 return 无法将负载解析为 json 无法识别的令牌

aws api gateway return Could not parse payload into json Unrecognized token

我正在尝试通过 api 网关向 aws lambda 函数发送 post 请求。

但是当我通过 postman 发送它时 return 出现错误 “无法将负载解析为 json”。 无法识别的令牌 如下图(我删除了一些无用的信息):

接下来我尝试通过 api 网关网页上的“测试方法”发送相同的请求: 它成功了! (我很确定使用相同的请求 body)

额外信息:

  1. 未在 Api 网关集成请求中选择“使用 Lambda 代理集成”。
  2. 尝试设置“映射模板”但没有解决我的问题。
  3. 我通过无服务器框架为 api 创建该资源。
  4. 请求 header Content-Type 已设置为“application/json”。
  5. 我已经检查了我的 lambda 函数的 CloudWatch,没有发现任何问题,证明问题仅限于 api 网关。

这是我的 lambda 代码:

def handle(event, context):
  res = {'code': 200}
  bad_res = {'code': 400}
  if event['operation'] == 'JoinCluster':
    # verify user by aws cognito
    is_allowed, user = is_joining_allowed(event)
    if is_allowed:
        # add a item in dynamodb
        client_info = client_table.add_requested_client(user, event["metadata"])
        # get a available token from another table
        token = token_table.get_valid_token()
        res = {'owner': user, 'id': client_info['id'], **token, **res}
        # Send a message to SQS to indicate that a node is ready to join
        aws.send_message({'id': client_info['id']}, queue_name="netmind-node-join-queue")
        return res
    else:
        bad_res["message"] = "You are not allowed to join this cluster, check your access token before retrying."
        return bad_res

  bad_res["message"] = "unknown operation."
  return bad_res

我在邮递员网站上用 api-gateway 尝试了你的 lambda,效果很好。

您上面没有提到的原因导致了问题。

在我的邮递员页面中,正文选项卡右侧有绿点。 我也选择 raw 并且 JSON 按照您的屏幕截图进行操作。

但对于参数和授权选项卡, 我不知道设置什么,让他们不开绿灯。 我猜是有什么东西弄错了。

以下是我的测试 api-gateway 定义,它适用于我的站点。 您应该先将 ARN_FOR_YOUR_LAMBDA 更改为您的真实 lambda-arn,然后才能导入到您的 api-gateway。

---
swagger: "2.0"
info:
  description: "test"
  version: "2022-04-02T15:18:02Z"
  title: "PetStore"
host: "HOST_OF_YOUR_API"
basePath: "/test"
schemes:
- "https"
paths:
  /mao:
    post:
      produces:
      - "application/json"
      responses:
        "200":
          description: "200 response"
          schema:
            $ref: "#/definitions/Empty"
      x-amazon-apigateway-integration:
        httpMethod: "POST"
        uri: "arn:aws:apigateway:ap-northeast-1:lambda:path/2015-03-31/functions/ARN_FOR_YOUR_LAMBDA/invocations"
        responses:
          default:
            statusCode: "200"
        passthroughBehavior: "when_no_match"
        contentHandling: "CONVERT_TO_TEXT"
        type: "aws"
    options:
      consumes:
      - "application/json"
      produces:
      - "application/json"
      responses:
        "200":
          description: "200 response"
          schema:
            $ref: "#/definitions/Empty"
          headers:
            Access-Control-Allow-Origin:
              type: "string"
            Access-Control-Allow-Methods:
              type: "string"
            Access-Control-Allow-Headers:
              type: "string"
      x-amazon-apigateway-integration:
        responses:
          default:
            statusCode: "200"
            responseParameters:
              method.response.header.Access-Control-Allow-Methods: "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'"
              method.response.header.Access-Control-Allow-Headers: "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'"
              method.response.header.Access-Control-Allow-Origin: "'*'"
        requestTemplates:
          application/json: "{\"statusCode\": 200}"
        passthroughBehavior: "when_no_match"
        type: "mock"
definitions:
  Empty:
    type: "object"

希望得到帮助。