我有一个有效的 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 内部服务器错误。
因此,在将我的示例减少到尽可能少的情况下发布问题时,我实际上偶然发现了答案。
{}
仍然是 content,必须使用 null
来响应 204。
令人恼火的是,无法返回无任何内容,这也是一个错误。
例如
#set($context.responseOverride.status = 204)
null ## turns out this is a minimal, valid JSON document
这让我有点困惑是由于两件事: