I used the 'AWS Mobile Hub' web site to create a Mobile Hub for my project and selected JS Web as the project type. I added Cloud Logic to the Mobile Hub using the web page.
In my React app, the authentication works, but when I try to call API.get(), I receive a CORS error 'Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource'.
I added the headers to /awsmobilejs/backend/cloud-api/itemsHandler/index.js
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true
},
My API call is as follows:
let apiName = "Fourty Two Forms";
let path = "/items";
let myInit = {
// OPTIONAL
headers: {} // OPTIONAL
//response: true // OPTIONAL (return entire response object instead of response.data)
};
return API.get(apiName, path, myInit);
// .then(response => {
// // Add your code here
// });
}
I have tried it with and without the init param, and I have also tried adding the headers to the myInit.
This syntax was from the API documentation.
If I try to enable cors on the API Gateway it errors. I assume this is because of the integration with mobile hub since it was auto-generated.
Is there a sample or tutorial on how to use API calls from Amplify from localhost for development with CORS enabled?
Thanks
I just started over and used "awsmobile cloud-api enable" from the command line, and was able to get it to work.
It looks to me like there is an issue with adding "Cloud Logic" to a mobile hub by using the web interface. It creates a different solution for the backend than if you use the CLI to add cloud-api.
@wesgrimes ,
Thanks for trying out Amplify and reporting this to us. We'll take a look into the issue with Mobile Hub and CORS policies for API Gateway.
I'm closing this for now, please feel free to re-open this if you have any questions.
Thanks.
The console now supports CORS for APIGW.
Examples of this working on your local? Just can't seem to get it to work.
Using Postman I can access the API Gateway fine, but not from my app.
Most helpful comment
Examples of this working on your local? Just can't seem to get it to work.
Using Postman I can access the API Gateway fine, but not from my app.