首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >发出OPTIONS请求时发生AWS API网关错误

发出OPTIONS请求时发生AWS API网关错误
EN

Stack Overflow用户
提问于 2019-04-13 23:29:15
回答 2查看 1.8K关注 0票数 4

我正在使用CFT为我的API创建环境。我已经添加了CORS的选项。我已经注意到,当我从AWS console for OPTIONS上做测试时,我得到了200响应。然而,当我在CURL or PostMan中执行同样的操作时,我得到了500内部服务器错误。在回顾了与之相关的SO问题之后。我已经将集成响应修改为CONVERT_TO_TEXT。但这也没有解决问题。

我在日志中注意到一种有线行为。以下是来自AWS控制台的请求的日志片段:

代码语言:javascript
运行
复制
Sat Apr 13 15:06:26 UTC 2019 : Method request headers: { Access-Control-Request-Method= POST, Content-Type= application/json}
Sat Apr 13 15:06:26 UTC 2019 : Method request body before transformations: 
Sat Apr 13 15:06:26 UTC 2019 : Method response body after transformations: 
Sat Apr 13 15:06:26 UTC 2019 : Method response headers: {X-Requested-With=*, Access-Control-Allow-Headers=Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-requested-with, Access-Control-Allow-Origin=*, Access-Control-Allow-Methods=POST,OPTIONS, Content-Type=application/json}
Sat Apr 13 15:06:26 UTC 2019 : Successfully completed execution
Sat Apr 13 15:06:26 UTC 2019 : Method completed with status: 200

但是,当我从CRUL或PM发出同样的请求时,我看到了以下日志:

代码语言:javascript
运行
复制
Method request path: {}
Method request query string: {}
Method request headers:  Method request headers: {Accept=*/*, CloudFront-Viewer-Country=IN, CloudFront-Forwarded-Proto=https, CloudFront-Is-Tablet-Viewer=false, CloudFront-Is-Mobile-Viewer=false, User-Agent=curl/7.55.1, X-Forwarded-Proto=https, CloudFront-Is-SmartTV-Viewer=false, Host=MYHOST, X-Forwarded-Port=443,   (CloudFront), Access-Control-Request-Method=POST, CloudFront-Is-Desktop-Viewer=true, Content-Type=application/json}
Method request body before transformations: [Binary Data]
Execution failed due to configuration error: Unable to transform request
Method completed with status: 500

我们可以看到它正在尝试转换[Binary Data],但我没有发送任何东西。

我使用的Curl是:curl -X OPTIONS -H "Access-Control-Request-Headers: Content-Type" -H "Access-Control-Request-Method: POST" -H "Access-Control-Allow-Origin: '*'" -v MYHOST

为什么我在日志中看到了这种差异?我的配置出了什么问题?你能帮帮我吗。

更新:我正在使用下面的CFT

代码语言:javascript
运行
复制
Type: AWS::ApiGateway::Method
Properties:
  AuthorizationType: NONE
  HttpMethod: OPTIONS
  Integration:
    Type: MOCK
    IntegrationResponses:
    - StatusCode: 200
      ResponseParameters:
        method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
        method.response.header.Access-Control-Allow-Methods: "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'"
        method.response.header.Access-Control-Allow-Origin: "'*'"
    RequestTemplates:
      application/json:
        Fn::Join:
        - ''
        - - "{"
          - ' {"statusCode":200} '
          - "}"
  MethodResponses:
  - StatusCode: 200
    ResponseParameters:
      method.response.header.Access-Control-Allow-Headers: true
      method.response.header.Access-Control-Allow-Methods: true
      method.response.header.Access-Control-Allow-Origin: true
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-09-14 16:43:41

似乎有一个文档较少的要求将contentHandling: CONVERT_TO_TEXT参数添加到两者:集成请求和集成响应设置。

在swagger CORS配置中,如下所示:

代码语言:javascript
运行
复制
responses: {
  200: {
    description: "Returning CORS headers",
    headers: {
      "Access-Control-Allow-Headers":{ type: "string" },
      "Access-Control-Allow-Methods": { type: "string" },
      "Access-Control-Allow-Origin": { type: "string" },
    }
  }
},
"x-amazon-apigateway-integration": {
  type: "mock",
  contentHandling: "CONVERT_TO_TEXT", // Resolves problems with cloudfront binary content issues
  requestTemplates: {
    "application/json": "{ \"statusCode\": 200 }"
  },
  responses: {
    "default": {
      statusCode: "200",
      contentHandling: "CONVERT_TO_TEXT", // Resolves problems with cloudfront binary content issues
      responseParameters: {
        "method.response.header.Access-Control-Allow-Headers": "'*'",
        "method.response.header.Access-Control-Allow-Methods" : "'*'",
        "method.response.header.Access-Control-Allow-Origin" : "'*'"
      },
      responseTemplates: {
        "application/json": "{}"
      }
    }
  }
}
票数 2
EN

Stack Overflow用户

发布于 2021-10-21 17:12:26

如果在你的API网关设置中,你已经将'application/json‘添加到了二进制媒体类型(比如你想要gzip响应的情况下),或者如果这个请求是针对二进制媒体类型的,那么由'enable CORS’功能添加的默认选项将需要调整。

正如上面所说的,至少对于集成来说,您需要将contentHandling设置为"CONVERT_TO_TEXT“。对我来说,它无需将其添加到集成响应中即可工作。您可以通过AWS CLI执行此操作,如下所示:

代码语言:javascript
运行
复制
aws apigateway update-integration --rest-api-id YOUR_REST_ID --resource-id YOUR_RESOURCE_ID --http-method OPTIONS --patch-operations op='replace',path='/contentHandling',value='CONVERT_TO_TEXT'

哦,之后别忘了重新部署API。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55667066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档