将 AWS API Gateway 集成响应转换为 204 无内容响应

问题描述 投票:0回答:1

我有一个有效的 AWS API Gateway 方法集成 它返回有效负载,我希望它根本不返回有效负载作为 HTTP 204 无内容响应。

该方法的工作版本的 CloudFormation 定义。它只是委托 SQS 发送消息。

# The rest of the Cloudformation template is boiler-plate API Gateway and elided.

  RestPost:
    Type: AWS::ApiGateway::Method
    Properties:
      RestApiId: !Ref RestApi
      ResourceId: !Ref RestResource
      AuthorizationType: NONE  # This method is public.
      HttpMethod: POST
      Integration:
        Type: AWS
        Credentials: !GetAtt Role.Arn
        IntegrationHttpMethod: POST
        Uri: !Sub arn:aws:apigateway:${AWS::Region}:sqs:path/${AWS::AccountId}/${Queue.QueueName}
        PassthroughBehavior: NEVER
        RequestParameters:
          integration.request.header.Content-Type: "'application/x-www-form-urlencoded'"
        RequestTemplates:
          application/json: Action=SendMessage&MessageBody=$util.urlEncode($input.body)
        IntegrationResponses:
          - StatusCode: 200  # This, the first one, is also the default match, so the code here means nothing
            ResponseTemplates:
              application/json: |
                {}  ## a minimal JSON document
      MethodResponses:
        - StatusCode: 200  # This, the first one, is also the default match, so the code here means nothing

启用X-Ray确实表明该方法存在错误,但我似乎无法从中提取任何其他有用的信息。

ResponseTemplate
更改为

#set($context.responseOverride.status = 202)
{}  ## a minimal JSON document

这可以正常工作并正确地将状态代码从 200 OK 映射到 202 Accepted。

ResponseTemplate
更改为

#set($context.responseOverride.status = 204)
{}  ## a minimal JSON document

这将返回 500 内部服务器错误。

amazon-web-services aws-cloudformation aws-api-gateway
1个回答
0
投票

因此,在将我的示例减少到尽可能少的情况下发布问题时,我实际上偶然发现了答案。

{}
仍然是 content,必须使用
null
来响应 204。

令人恼火的是,无法返回无任何内容,这也是一个错误。

例如

#set($context.responseOverride.status = 204)
null  ## turns out this is a minimal, valid JSON document

这让我有点困惑是由于两件事:

  1. 使用 localstack 进行本地测试是非常宽松的(它的行为与 AWS 本身不一致),它的日志中没有任何内容可以提供帮助,因为它认为这是可以的
  2. 我仍然不确定如何在AWS-proper中找到这个错误。
© www.soinside.com 2019 - 2024. All rights reserved.