[英]AWS API gateway giving 400 bad request error
我正在使用 API 網關 + 基於 JWT 的授權來保護我的 AWS lambda 函數。 我能夠在我的 aws 控制台中為授權人獲得正確的策略。 我試圖訪問的 lambda 函數單獨運行正常。 當我將GET_CUSTOMERS_URI
授權更改為 none 並獲取數據時,它工作正常。 但是當使用 API 網關組合在一起時,它會給出 400 個錯誤的請求錯誤。 我知道這與如何從前端發出請求有關,但我無法弄清楚。
我的獲取請求看起來像這樣[React app]:-
return await fetch(GET_CUSTOMERS_URI, {
method: 'GET',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'authorizationToken': 'Bearer ' + token,
'Content-Type': 'application/json'
},
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer',
})
從授權方返回的策略(使用 AWS 控制台測試):-
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "execute-api:Invoke",
"Effect": "Allow",
"Resource": "arn:aws:execute-api:us-west-2:<account id>:<api id>/<stage-name>/GET/customers"
}
]
}
事實證明這是一個 CORS 錯誤。 我通過以下方式解決了這個問題:-
Actions
-> Enable CORS
Access-Control-Allow-Headers
字段設置為以逗號分隔的所有值,您將作為前端的標頭傳遞Enable CORS and replace existing CORS headers
按鈕
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.