简体   繁体   English

CORS 仅 POST 请求错误:AWS HTTP API 网关

[英]CORS Error for Only POST request: AWS HTTP API Gateway

So my AWS HTTP API GATEWAY is integrated to a Lambda Function.所以我的AWS HTTP API GATEWAY被集成到一个Lambda Function。

Here's my CORS configuration On AWS.这是我在 AWS 上的 CORS 配置。 CORS配置

My GET & POST(without body) requests are working fine.我的 GET 和 POST(无正文)请求工作正常。 在此处输入图像描述

But when I make a POST request with some data/body, it has a CORS error.但是当我用一些数据/主体发出 POST 请求时,它有一个 CORS 错误。

Access to XMLHttpRequest at 'https://api_url/test' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

I understand how the browser sends an OPTIONS request before the Original POST request to the server.我了解浏览器如何在原始 POST 请求之前向服务器发送 OPTIONS 请求。

Here's the CORS error with the OPTIONS request.这是 OPTIONS 请求的 CORS 错误。 在此处输入图像描述

Here's how i call my request from a react app using axios.以下是我如何使用 axios 从反应应用程序调用我的请求。

const res_post = axios.post(`${process.env.REACT_APP_AWS_API_GATEWAY}/test`, {key: "value"}).then((res) => {
    console.log("AXIOS POST REQUEST 2 RESULT", res, res.data);
});

I tried adding hearders to the axios call, No Luck.我尝试在 axios 呼叫中添加听众,运气不好。 I added a route(for / & /test) for a OPTIONS request, as suggested by some answers, unfortunately No luck.正如一些答案所建议的那样,我为 OPTIONS 请求添加了一条路线(for / & /test),不幸的是没有运气。 在此处输入图像描述

I have spent over 4 hours figuring out and applying all the solutions on stackoverflow.我花了 4 个多小时找出并应用了 stackoverflow 上的所有解决方案。 I will appreciate if someone can point me the right direction.如果有人能指出正确的方向,我将不胜感激。 I have read multiple stackoverflow questions and applied several of them.我已经阅读了多个 stackoverflow 问题并应用了其中的几个。

Refer to this: Vue & Axios + AWS API Gateway & Lambda - CORS + POST not working参考这个: Vue & Axios + AWS API Gateway & Lambda - CORS + POST not working

Even though I added a way to handle OPTIONS requests.即使我添加了一种处理 OPTIONS 请求的方法。 My lambda function was returning and empty response.我的 lambda function 正在返回并且响应为空。 I fixed that and it worked.我修好了,它起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS API 网关 HTTP API CORS 预检请求 - AWS API Gateway HTTP API CORS preflight request XMLHttpRequest 错误 flutter web [启用 CORS AWS API 网关] - XMLHttpRequest error in flutter web [Enabling CORS AWS API gateway] 如何使用 Lambda 访问 AWS API Gateway 请求的 HTTP 标头? - How to access HTTP headers for request to AWS API Gateway using Lambda? AWS API 网关:CORS 和空事件 Object - AWS API Gateway : CORS and Empty Event Object CORS 错误:即使我在上传到 Lamda 时在 API 网关和我的 index.js 文件中启用它 function AWS - CORS error: even i enable it at API-Gateway and in my index.js file when uploading to Lamda function AWS 尽管设置了标头,但调用 API 网关返回 CORS 错误 - Call to API Gateway returning CORS error despite headers being set HTTP 发布 API 请求,使用 go 语言和 AWS 机密管理器 Z099FB995346F31C749F6E0E4 - HTTP Post API request using go lang with aws secrets manager header 尝试使用 node.js Lambda 函数在 AWS REST API 配置上使用 Axios 发布数据时出现 CORS 错误 - CORS error while trying to post data with Axios on AWS REST API configuration using a node.js Lambda function AWS API 网关缓慢 - AWS API Gateway Slowness 对亚马逊 MWS API 的 Python HTTP POST 请求 - Python HTTP POST Request to Amazon MWS API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM