【问题标题】:All GET methods in API gateway REST API returns 403 and HTML contentAPI 网关 REST API 中的所有 GET 方法都返回 403 和 HTML 内容
【发布时间】:2022-10-21 04:32:15
【问题描述】:

我已经通过 CloudFormation 设置了一个具有 OpenAPI 规范和 Lambda 集成的 API 网关。它已正确部署并显示所有方法和资源等。 如果我调用我的 POST、PATCH 或 DELETE 方法之一(甚至是 OPTIONS!),它们会成功到达我的 lambda 函数并按预期返回。 但是,一旦我使用 GET 或 HEAD 方法调用我的一个端点——无论是存在的 GET 端点,还是不存在的 GET 方法(例如 /foobar),我得到的只是一个 HTTP 403HTML内容而不是 JSON:

<!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.
We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
<BR clear="all">
If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.
<BR clear="all">
<HR noshade size="1px">
<PRE>
Generated by cloudfront (CloudFront)
Request ID: ju-yNp9QlyjqrAFHC3xR9DkO0N9DaPK2BcVQlpeswPMEerwErwdDUw==
</PRE>
<ADDRESS>
</ADDRESS>
</BODY></HTML>

标头仅包括X-Cache: Error from Cloudfront,没有常规API 网关标头,例如x-amzn-ErrorType。我相信该请求甚至没有到达我的 API 网关。

我正在调用我的xxxxxxxx.execute-api.eu-west-1.amazonaws.com URL,所以 CloudFront 没有手动放在前面,但我猜 API 网关本身使用 CloudFront。是否因某种原因为我的 GET 端点启用了缓存?在我的 API 网关阶段参数中,“API 缓存”被禁用,我找不到与 CloudFront 或缓存远程相关的任何其他内容。

我的 API 及其集成如下所示:

  /mypath:
    get:
      x-amazon-apigateway-integration:
        type: aws_proxy
        passthroughBehavior: when_no_match
        httpMethod: POST
        uri: arn:aws:apigateway:eu-west-1:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-west-1:xxxxxxxxx:function:${stageVariables.LambdaName}/invocations
        requestParameters: {}
        responses:
          '200':
            statusCode: '200'
          '400':
            statusCode: '400'

这种集成适用于所有非 GET 请求,但会导致所有 GET 请求出现这种奇怪的错误。 我有 DEFAULT_4XX 响应,这也适用于所有其他方法,例如 POST /something-non-existing - 这使得我的自定义响应很好。我没有在任何地方配置 HTML 内容,为什么会发生这种情况?

【问题讨论】:

    标签: aws-api-gateway amazon-cloudfront


    【解决方案1】:

    事实证明,只有当我尝试使用请求正文执行 GET 时才会发生这种情况。我正在使用 Postman,并且意外地保留了之前 POST 请求的请求正文。当我删除正文时,GET 请求起作用。

    我还没有找到任何方法来防止对“GET 或 HEAD 不允许的正文”进行这种奇怪的处理。

    【讨论】:

      猜你喜欢
      • 2020-05-25
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 2016-09-24
      • 2016-07-27
      相关资源
      最近更新 更多