serverless-express: [Question] Internal server error when request method OPTIONS
Had anyone same issue? When I try via postman make a request method OPTIONS the response is
{"message": "Internal server error"}
if I try in API Gateway Console, lambda Console, npm run local and npm run invoke-lambda the response is ok. Any idea what is the problem?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 5
- Comments: 26 (3 by maintainers)
Commits related to this issue
- Remove error when we options method request Solution from this link: https://github.com/awslabs/aws-serverless-express/issues/58 — committed to WorldBrain/analytics-backend by mukeshkharita 6 years ago
- Use contentHandling: CONVERT_TO_TEXT for CORS mock integrations If an API uses contentHandling: CONVERT_TO_BINARY anywhere, the generated mock integrations for CORS would fail with a 500 error. This... — committed to henhal/serverless-openapi-integration-helper by henhal a year ago
- Use contentHandling: CONVERT_TO_TEXT for CORS mock integrations If an API uses contentHandling: CONVERT_TO_BINARY anywhere, the generated mock integrations for CORS would fail with a 500 error. This... — committed to henhal/serverless-openapi-integration-helper by henhal a year ago
With the help from staff on AWS forums, I’ve resolved it by adding:
which now returns a correct 200 response for the OPTIONS request. It wasn’t obvious we needed to do that however, especially that this option is not available for Mock requests through the Console yet.
@samikarak that needs to be added to the configuration of your OPTIONS methods in the Swagger template (https://github.com/awslabs/aws-serverless-express/blob/master/example/simple-proxy-api.yaml#L43)
If you’re not using swagger, which I wasn’t, a couple things solved the issue for me. First was enabling CORS in the serverless.yml
The second was making sure my Express setup allowed the OPTIONS method through:
Hope this helps some people out. It took me a bit to figure out all the pieces.
@brettstack would you explain this" remove the OPTIONS method and redeploy your API and let Express deal with CORS."
Thanks @Jun711. This has been merged into develop branch and we’ll merge into master soon. Closing.
Okay I’ll leave this open until I can confirm. Alternative is to simply remove the OPTIONS method from the Swagger template and let Lamdba handle the preflight request. Having the OPTIONS method is a performance enhancement (i.e. the response will be faster since it’s terminated at the API Gateway layer)
I’ll verify this later. For now, simply remove the OPTIONS method and redeploy your API and let Express deal with CORS.