Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Internal server error when request method OPTIONS #58

Closed
miguelslemos opened this issue Apr 12, 2017 · 26 comments
Closed

[Question] Internal server error when request method OPTIONS #58

miguelslemos opened this issue Apr 12, 2017 · 26 comments

Comments

@miguelslemos
Copy link

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?

@miguelslemos
Copy link
Author

Execution log for request test-request
Wed Apr 12 12:24:37 UTC 2017 : Starting execution for request: test-invoke-request
Wed Apr 12 12:24:37 UTC 2017 : HTTP Method: OPTIONS, Resource Path: /flowrates
Wed Apr 12 12:24:37 UTC 2017 : Method request path: {proxy=flowrates}
Wed Apr 12 12:24:37 UTC 2017 : Method request query string: {}
Wed Apr 12 12:24:37 UTC 2017 : Method request headers: {}
Wed Apr 12 12:24:37 UTC 2017 : Method request body before transformations: 
Wed Apr 12 12:24:37 UTC 2017 : Endpoint response body before transformations: 
Wed Apr 12 12:24:37 UTC 2017 : Endpoint response headers: {}
Wed Apr 12 12:24:37 UTC 2017 : Method response body after transformations: {}

Wed Apr 12 12:24:37 UTC 2017 : Method response headers: {Access-Control-Allow-Origin=*, Access-Control-Allow-Methods=POST,GET,OPTIONS, Access-Control-Allow-Headers=Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token, Content-Type=application/json}
Wed Apr 12 12:24:37 UTC 2017 : Successfully completed execution
Wed Apr 12 12:24:37 UTC 2017 : Method completed with status: 200

@vitalii-budkevych
Copy link

OPTIONS works just fine for me from postman. Couple potential reasons for "Internal server error":

  1. Stage you're calling from postman has deployment that differs from current version of the API invoked by test invoke and perhaps OPTIONS method doesn't exist in the deployment on the stage.
  2. Your postman request includes some unexpected headers and/or doesn't include expected headers. Something like {"Content-Type":"application/x-www-form-urlencoded"} instead of {"Content-Type":"application/json"}

@miguelslemos
Copy link
Author

everything is ok, only way to stop {"message": "Internal server error"} is remove options from simple-proxy-api

@dinvlad
Copy link

dinvlad commented May 15, 2017

Any way to resolve it? I've tried setting up options exactly as in the example, but still getting the same error. I suspect it has something to do with binary support. I.e. if I remove the binary types from the API, OPTIONS starts to work correctly. If I keep binary support, I get 500 even though I set both 'Content-Type' and 'Accept' to 'application/json' in my client requests (done both from Postman and from cURL). I am also not sure we are supposed to send 'Content-Type' in pre-flight OPTIONS anyway, only in subsequent requests.

@brettstack
Copy link
Collaborator

I'll verify this later. For now, simply remove the OPTIONS method and redeploy your API and let Express deal with CORS.

@brettstack brettstack reopened this May 16, 2017
@dinvlad
Copy link

dinvlad commented May 22, 2017

With the help from staff on AWS forums, I've resolved it by adding:

x-amazon-apigateway-integration:
  contentHandling: CONVERT_TO_TEXT
  ...

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
Copy link

@dinvlad

cloud you please tell us wehere to add this configuration exactly ? thanks !

@dinvlad
Copy link

dinvlad commented Aug 14, 2017

@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)

@blackc0d3
Copy link

Hope this helps... In my case i had two functions with similar names: 'user-get-all' and 'user-get', defined in that order in the serverless.yml file. The error message i was getting was "Internal Server Error" + "Execution failed due to configuration error: Invalid permissions on lambda function. Method completed with status 500". After changing the name of the secon function to "user-get-item" it was solved. (Perhaps changing the order of the functions in the serverless.yml might work too).

Regards,

George

@individual11
Copy link

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

functions:
  api:
    handler: lambda.handler
    events:
      - http:
          path: /
          method: ANY
          cors: true
      - http:
          path: /{proxy+}
          method: ANY
          cors: true

The second was making sure my Express setup allowed the OPTIONS method through:

app.use( (req, res, next) => {
   res.header("Access-Control-Allow-Origin", "*")
   res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS")
   res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
   next();
 })

Hope this helps some people out. It took me a bit to figure out all the pieces.

@chriszrc
Copy link

chriszrc commented May 7, 2018

@dinvlad Why hasn't the file you link to just been updated with the necessary fixes? Seems like this would save people a lot of headaches-
(https://github.com/awslabs/aws-serverless-express/blob/master/example/simple-proxy-api.yaml#L43)

@dinvlad
Copy link

dinvlad commented May 8, 2018

@chriszrc I'm not in charge of this package, sorry

@chriszrc
Copy link

chriszrc commented May 8, 2018 via email

@Jun711
Copy link
Contributor

Jun711 commented May 24, 2018

@dinvlad Do u happen to know if this can be done via API gateway console?
I only use SAM to deploy to lambda but not API Gateway.

I removed binary type * / * and OPTIONS method works using Postman. However, my audio/mp3 type doesn't work if I don't set binary type as * / *

@dinvlad
Copy link

dinvlad commented May 24, 2018

@Jun711 haven't worked with SAM in a while, we've migrated to Firebase, sorry

@brettstack
Copy link
Collaborator

Reopening this to track fixing the example https://github.com/awslabs/aws-serverless-express/blob/master/example/simple-proxy-api.yaml#L43. Easy first PR for someone :)

@Jun711
Copy link
Contributor

Jun711 commented Jul 5, 2018

@brettstack
I noticed that even though CONVERT_TO_TEXT is specified on the template, it is not set to CONVERT_TO_TEXT on the console for OPTIONS method. I had to manually change it on the console.

@brettstack
Copy link
Collaborator

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)

@dinvlad
Copy link

dinvlad commented Jul 6, 2018

It can also be a security mechanism, i.e. we prevent all unauthenticated access to Lambdas (in case there are any exploits that could be executed through OPTIONS in Lambda code/dependencies).

@JontyC
Copy link

JontyC commented Jul 20, 2018

Seeing the same as @Jun711 - Is the issue not that this should be also added to https://github.com/awslabs/aws-serverless-express/blob/master/examples/basic-starter/simple-proxy-api.yaml#L87

mukeshkharita added a commit to WorldBrain/analytics-backend that referenced this issue Jul 20, 2018
@brettstack
Copy link
Collaborator

@Jun711 revisiting this I realized your PR only modified the / OPTIONS and missed the /{proxy+} OPTIONS.

@Jun711
Copy link
Contributor

Jun711 commented Aug 27, 2018

@brettstack ok, will do. Has the folder structure been changed?

@brettstack
Copy link
Collaborator

Yes it's now in examples/basic-starter

@Jun711 Jun711 mentioned this issue Aug 27, 2018
@brettstack
Copy link
Collaborator

Thanks @Jun711. This has been merged into develop branch and we'll merge into master soon. Closing.

@Aneez-Zahra
Copy link

@brettstack would you explain this" remove the OPTIONS method and redeploy your API and let Express deal with CORS."

@gham-khaled
Copy link

With the help from staff on AWS forums, I've resolved it by adding:

x-amazon-apigateway-integration:
  contentHandling: CONVERT_TO_TEXT
  ...

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.

By adding the contentHandling I am getting the Execution failed due to configuration error: statusCode should be an integer which defined in request template .

henhal added a commit to henhal/serverless-openapi-integration-helper that referenced this issue Mar 28, 2023
If an API uses contentHandling: CONVERT_TO_BINARY anywhere, the generated mock integrations for CORS would fail with a 500 error.

This turned out to be due to a missing CONVERT_TO_BINARY attribute.
Ref: CodeGenieApp/serverless-express#58 (comment)
henhal added a commit to henhal/serverless-openapi-integration-helper that referenced this issue Mar 28, 2023
If an API uses contentHandling: CONVERT_TO_BINARY anywhere, the generated mock integrations for CORS would fail with a 500 error.

This turned out to be due to a missing CONVERT_TO_BINARY attribute.
Ref: CodeGenieApp/serverless-express#58 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests