簡體   English   中英

CORS POST 失敗,AWS HTTP API 網關 + Lambda 集成

[英]CORS POST fail with AWS HTTP API Gateway + Lambda integration

我已經搜索了超過 25 篇關於此的 SO 帖子,但找不到解決我的問題的方法。 I have an API Gateway with an HTTP API + route that utilizes a Lambda function integration. 從 SO 帖子和 AWS 文檔中,我正在閱讀相互矛盾的信息:

  1. AWS 文檔中,我看到

If you configure CORS for an API, API Gateway automatically sends a response to preflight OPTIONS requests, even if there isn't an OPTIONS route configured for your API. 對於 CORS 請求,API 網關將配置的 CORS 標頭添加到來自集成的響應中。

如果您為 API 配置 CORS,則 API 網關將忽略從您的后端集成返回的 Z5A8FEFF0B4BDE3EEC9244B76023B79 標頭。

這正是我所做的; 我將我的CORS配置如下:

但是,我仍然收到錯誤:

Access to fetch at 'https://domain.execute-api.aws-region.amazonaws.com/dev/upload' from
 origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight 
request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is 
present on the requested resource. If an opaque response serves your needs, set the 
request's mode to 'no-cors' to fetch the resource with CORS disabled.

從 Cloudwatch,我可以看到請求被發送到 API,但由於某種原因,header 配置不正確。 我不明白這一點,因為我認為 API 網關應該處理 HTTP API 的 OPTIONS 飛行前請求。 我無法找出我做錯了什么。

我有一個類似的問題,預檢響應沒有 CORS 標頭。 我注意到預檢請求包括 header: Access-Control-Request-Headers: Content-Type

一旦我將Content-Type添加到 HTTP API 配置中的Access-Control-Allow-Headers列表中,它就對我有用。

如果為 API 配置 CORS,則 API 網關會自動向預檢 OPTIONS 請求發送響應

我認為該規則不適用於 POST 方法,因為我遇到了同樣的問題。 然后我嘗試手動添加一個 OPTIONS 路由,一切正常。 路線圖

暫無
暫無

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

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