CORS is not working and doesn't support httpAPI
According to this guide by Alex Debrie
I implemented my serverless.yml
functions:
hello:
handler: handler.fileUpload
events:
- http:
path: /sendfile
method: post
cors: true
and lambda function as
module.exports.fileUpload = async (event) => {
const body = JSON.stringify(event.body);
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers" : "*",
},
body: body,
}
};
I deployed it and it works on postman but not in browser since I'm trying in my local machine giving me the error below
Access to XMLHttpRequest at 'https://{uniqueString}.amazonaws.com/sendfile' from origin 'http://localhost:5500' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
what should I do to get this working?
And one more thing is that the CORS method described in that blog doesn't works for httpAPI and gives me this warning :
Configuration warning at 'functions.hello.events[0].httpApi': unrecognized property 'cors'
if I happen to use it in following way
functions:
hello:
handler: handler.fileUpload
events:
- httpApi:
path: /sendfile
method: post
cors: true
Hello @MeRahulAhire :wave:
As for the first part - I think this is more suited to be discussed on our Slack or forum -https://forum.serverless.com/ as I'm not sure if this is a bug with the Framework or with your configuration - you might also want to checkout our docs for that https://www.serverless.com/framework/docs/providers/aws/events/apigateway/#enabling-cors
As for the httpApi - the configuration is a bit different - please refer to our docs: https://www.serverless.com/framework/docs/providers/aws/events/http-api/#cors-setup
Please let me know if you feel like we still might have a bug in the Framework that should be fixed, otherwise, let's close this issue as it's intended for bugs and enhancement proposals.
Thanks :bow:
Hi @pgrzesik, sorry for late response. I was engaged in other stuff
but anyways the error is still there in Browser whether you use CORS or not in serverless.yml.Its working in postman with/without any CORS

Hello @MeRahulAhire - I did not manage to reproduce you issue with the exact same example you've posted both with using cURL as well as calling with fetch from the browser. Maybe there's a configuration problem with how you're calling your endpoint from the browser? Anyway - I believe it's not an issue with the framework but with your individual configuration - in this case, I think it would be best to move to our forum or to Slack.
@pgrzesik I think I got what actually the issue is. the CORS property in serverless.yml wont work alone. You do need a headers in lambda function and ironically what I saw is whenever any error occurs it just throws CORS error in browser regardless of what it actually was. I think I do need to implement error handling correctly
@pgrzesik
I'm trying to "fine tune" cors as described in https://www.serverless.com/framework/docs/providers/aws/events/http-api/#cors-setup, except I'm using a typescript serverless configuration file and the type definitions are complaining.
Is it not possible to do with the typescript version yet?
I'm using latest type definitions from @types/serverless

Hello @brothatru - you might want to try: https://www.npmjs.com/package/@serverless/typescript - I'm not sure if the definitions in types repo are up-to-date at this point. The ones from linked repository are generated based on our validation schema so they should be accurate.
@pgrzesik Awesome @serverless/typescript package solves the issue! Thanks so much!
Happy to hear it helped to solve your problem @brothatru :raised_hands:
I don't know if anyone else is still having this issue, but I was able to get this to work by doing this 3 steps
- Adding preflight cors handlers
// Handle preflight CORS request
if (event.httpMethod === 'OPTIONS') {
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS,POST',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Credentials': 'true',
},
body: JSON.stringify({}),
};
}
- Ensuring that all API responses return the access control headers alongside the response
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': 'true',
},
body: JSON.stringify({ message: 'Contact Form data stored successfully' })
};
- I changed the serverless.yml function handler event from
httpApitohttp
from httpApi
functions:
contact:
handler: contact.handler
events:
- httpApi:
path: /contact
method: post
cors: true
to http
functions:
contact:
handler: contact.handler
events:
- http:
path: /contact
method: post
cors: true