I'm working with AWS API Gateway and I'm getting the following error:
Uncaught TypeError TypeError: Failed to fetch
My API is just returning a simple JSON string. I can get a successful response with curl, Postman and testing via the AWS console. But, using JavaScript, I get an error. I suspect that AWS requires a specific header
I separated the code from the original website. Some unnecessary parts have been removed.
<script type = "text/javascript">
const GET_url = '<url>'
const POST_url = '<url>'
async function getInvokeFromAPI() {
const response = await fetch(GET_url, {
method: 'GET'
});
const data = await response.json();
}
async function postInvokeFromAPI() {
const respone = await fetch(POST_url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
});
const data = await response.json();
}
</script>
It would be helpful to see the console output when the error happens.
But if it works with Postman and not with the fetch API, you are probably having a CORS error.
You can check this page of the MDN for more informations about CORS in case it is your error: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
If it is your error, it will be logged in the browser console in the dev tools.