CORS is really driving me crazy and I\'m really out of ideas as of what to try to make it work.
I have created a simple APIG Api with 1 resource cal
Ok, I found the origin of the problem, which happens to be totally unrelated to APIG, and confirms what @AbhignaNagaraja mentioned, that my APIG was properly configured.
The issue is actually in the way I called jQuery.ajax, which I thought was smart enough to convert my parameters to a JSON string when contentType is 'application/json'. It seems I had to manually stringify the JSON params rather than passing a JSON and having jQuery stringify it.
So this is the bad call:
$.ajax({
url: myEndpoint,
type: 'POST',
crossDomain: true,
data: {
url: $('#url').val()
},
headers: {
"X-Api-Key": 'blablabla'
},
dataType: 'json',
contentType: "application/json",
success: function (data) {
console.info(data);
}
});
And this is the right call:
$.ajax({
url: myEndpoint,
type: 'POST',
crossDomain: true,
data: JSON.stringify({
url: $('#url').val()
}),
headers: {
"X-Api-Key": 'blablabla'
},
dataType: 'json',
contentType: "application/json",
success: function (data) {
console.info(data);
}
});
This can be a hint if you are debugging such an issue with CORS: just download the AWS APIG SDK and try executing the call using the apigClient provided by AWS and compare headers with the ones you get with your custom client. When examining the 2 sets of headers I got with jQuery and apigClient, I noticed the Request Payload looked different and thats how I realized the format was wrong, then the 400 code and the No 'Access-Control-Allow-Origin' header is present all made sense.
I hope this helps.