正文已被CloudFront拒绝的AWS GET请求 [英] AWS GET request with body rejected by CloudFront

查看:461
本文介绍了正文已被CloudFront拒绝的AWS GET请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用合同由第三方定义的AWS SAM(Lambda和API网关)构建API。

I'm build an API using AWS SAM (Lambda & API Gateway) whose contract is defined by a 3rd party.

第三方通过以下方式调用我的API:主体中包含JSON的GET请求。但是,当带有正文的请求发送到API时,CloudFront会拒绝它。

The 3rd party calls my API with a GET request that contains JSON in the body. However, when a request is sent to the API with a body it gets rejected by CloudFront.

这是请求:

curl -X GET -H "Content-Type: application/json" --data '{"hello":"world"}' https://my-api.execute-api.us-east-2.amazonaws.com/Prod/my-api

这是响应:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
    <HEAD>
        <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
        <TITLE>ERROR: The request could not be satisfied</TITLE>
    </HEAD>
    <BODY>
        <H1>403 ERROR</H1>
        <H2>The request could not be satisfied.</H2>
        <HR noshade size="1px">
Bad request.


        <BR clear="all">
        <HR noshade size="1px">
        <PRE>
Generated by cloudfront (CloudFront)
Request ID: 1p0St_-e3noQL-2uMxeB_2I6lkMr1mg5afvxJRmVpCdnG67Vgnhj9w==
</PRE>
        <ADDRESS></ADDRESS>
    </BODY>
</HTML>

检查日志,该请求从不访问API网关或Lambda函数。但是,如果我从请求中删除主体,则它将触发Lambda函数,并且会从API中获得相应的错误消息(告诉调用方期望的主体丢失了。)

Checking the logs, the request never hits API Gateway or the Lambda function. However, if I remove the body from the request, then it hits the Lambda function and I get the appropriate error message from the API (telling the caller that the expected body is missing.)

curl -X GET -H "Content-Type: application/json" https://my-api.execute-api.us-east-2.amazonaws.com/Prod/my-api

我正在通过SAM模板使用API​​ Gateway的基本配置。这是相关的部分:

I'm using basic configuration of API Gateway via a SAM template. This is the relevant section:

MyApiFunction:
  Type: AWS::Serverless::Function
  Properties:
    CodeUri: bin/main.zip
    Handler: main
    Runtime: go1.x
    Tracing: Active
    Role: !Sub ${MyApiLambdaExecutorRole.Arn}
    Events:
      CatchAll:
        Type: Api
        Properties:
          Path: /my-api
          Method: GET


推荐答案

GET 请求不能包含请求正文。尝试改用 POST

GET requests cannot contain a request body. Try using POST instead.

如果要通过 GET 请求,可以使用查询参数。

If you want to send limited data in a GET request, you can use query parameters.

您可以在AWS Lambda文档中看到这是不可能的:

You can see in the AWS Lambda Docs that this isn't possible here:

https://docs.aws.amazon。 com / apigateway / latest / developerguide / getting-started-lambda-non-proxy-integration.html


您提交的GET请求无法获取有效负载,因此无法通过请求验证。

This is because the GET request that you submitted cannot take a payload and fails the request validation.

您也许可以使用不使用API​​ Gateway来处理请求的EC2实例或其他服务。

You could perhaps use an EC2 instance or other service that doesn't use API Gateway to handle the request.

这篇关于正文已被CloudFront拒绝的AWS GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆