Sadly another CORS Question (API Gateway, S3, React Amplify)

Hello Guys,
I am sitting on a Problem for quite some time now. I have the feeling that i have searched ever web page there is on CORS but I still can’t find out what i am doing wrong.

So the Basic parameters are I have a react app deployed with amplify.
From this app i want to call an API that will simply be able to SET a JSON file in S3 and READ that file.
The reading part is simple.

fetch(
“https://xxxxxxxx.execute-api.us-east-1.amazonaws.com/default/static”
)
.then((res) => res.json())
.then(
(result) => {
this.setState({
isLoaded: true,
items: result,
});
},
(error) => {
this.setState({
isLoaded: true,
error,
});
console.log(error);
}
);

This works like a charm. But now when it comes to the PUT Methode it is getting tricky.

const requestOptions = {
method: “PUT”,
mode: “cors”,
headers: {
“Content-type”: “application/json”,
“Access-Control-Allow-Headers”:
“Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token”,
“Access-Control-Allow-Methods”: “GET,OPTIONS,POST,PUT”,
“Access-Control-Allow-Origin”: “*”,
},
body: ‘{“test”:“test”}’,
};
fetch(
“https://xxxxxxxx.execute-api.us-east-1.amazonaws.com/default/static”,
requestOptions
)
.then((res) => {
res.json();
})
.then(
(result) => {
console.log(result);
},
(error) => {
console.log(“err”, error);
}
);

For some reason I am not able to get around the error:

Access to fetch at ‘https://xxxxxxxx.execute-api.us-east-1.amazonaws.com/default/static’ 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 have enabled CORS in the API gatway and I tried sending all different kinds of headers. And configurations. Is there any magic trick to make it work like it will on postman?
Thanks in advance!!!

Do you see options method in your resource ? A

And allow … origin has to be set by the api gateway not by your app.

The response from api gateway should have that header.

Do you see options method in your resource ? A

Yes, it appeared after “Enabled CORS”.

The API Gatway must have CORS set nunless there is something i am missing besided “Enable CORS”

The response from api gateway should have that header.

Under “OPTION - INtegration Response” I have this configuration for the Header Mappings:

Access-Control-Allow-Headers ‘Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token’
Access-Control-Allow-Methods ‘GET,OPTIONS,POST,PUT’
Access-Control-Allow-Origin ‘*’

I
First remove all unnecessary headers in request.

In your lambda response add allow origin * header and you Re all set

The CORS headers are response headers. Don’t set them in your request. You OPTIONS response status isn’t 2xx so it fails the preflight check. Ensure you don’t have an authorizer set on it. If you have a default authorizer ensure you explicitly set the authorizer to NONE on your OPTIONS method.