簡體   English   中英

發出 OPTIONS 請求時出現 AWS API 網關錯誤

[英]AWS API gateway error while making OPTIONS request

我正在使用 CFT 為我的 API 創建環境。我已經為 CORS 添加了選項。我注意到當我從AWS console for OPTIONS進行測試時,我得到了200響應。 但是,當我從CURL or PostMan執行相同操作時,我收到500內部服務器錯誤。 在審查了與之相關的SO問題之后。 我已將集成響應修改為 CONVERT_TO_TEXT。 但這也沒有解決問題。

我注意到日志中有一個有線行為。 以下是來自 AWS 控制台的請求的日志片段:

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 發出同樣的請求時,我看到了以下日志:

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

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

contentHandling: CONVERT_TO_TEXT參數添加到兩個:集成請求和集成響應設置中的要求較少。

在 swagger CORS 配置中看起來像這樣:

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": "{}"
      }
    }
  }
}

對我來說,問題來自這一行:

"x-amazon-apigateway-binary-media-types" : [ "multipart/form-data", "application/zip", "*/*" ]

只需刪除"*/*"

"x-amazon-apigateway-binary-media-types" : [ "multipart/form-data", "application/zip" ]

發現這個之后才看到@bgw的評論

無論如何,希望這可以幫助其他不搜索時間的人!

如果在您的 API 網關設置中,您已將“application/json”添加到二進制媒體類型(例如,如果您想要 gzip 響應),或者如果此請求是針對二進制媒體類型的,則由 ' 添加的默認 OPTIONS需要調整啟用 CORS 的功能。

就像上面所說的那樣,至少對於集成,您需要將 contentHandling 設置為“CONVERT_TO_TEXT”。 對我來說,它無需將其添加到集成響應中即可工作。 您可以通過 AWS CLI 執行以下操作:

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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM