I’ve seen many posts on this topic, but all of them deal with the CORS error message about missing headers and such.
I’m facing a different issue (I think…) - I’m getting a valid 200 response that looks like this:
Response {type: ‘cors’, url: ‘https://…amazonaws.com/dev/calculate’, redirected: false, status: 200, ok: true, …} body: ReadableStream bodyUsed: false headers: Headers {} ok: true redirected: false status: 200 statusText: “” type: “cors” url: “https://…amazonaws.com/dev/calculate”
The details:
I have a simple React app that uses a AWS Lambda function to calculate a sum of 2 numbers. I used Serverless to deploy this to AWS. The *.yml looks like so:
service: reactcalculatorserverless
frameworkVersion: ‘3’provider:
name: aws
runtime: nodejs12.xfunctions:
doCalculation:
handler: lambdas/doCalculation.handler
events:
- http:
path: calculate
method: POST
cors: true
The Lambda:
module.exports.handler = async (event) => {
const { operand1, operand2, operator } = JSON.parse(event.body); let result = 0; switch (operator) { case "+": result = operand1 + operand2; break; case "-": result = operand1 - operand2; break; case "*": case "X": case "x": result = operand1 * operand2; break; case "/": result = operand1 / operand2; break; } const responseBody = { "result" : result }; const response = { "statusCode": 200, "headers": { "Access-Control-Allow-Origin" : "*", "Access-Control-Allow-Credentials": true, }, "body": JSON.stringify(responseBody), "isBase64Encoded": false }; return response;
Of course this all works well with Postman.
Is this a CORS error, even though it is a valid response header? Since I have “cors=true” in the yml file, it did do the right thing and added the OPTIONS in API Gateway, but still I don’t get the correct response from the Lambda.