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!!!