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

CORS causes PutMethod operation: Method already exists ... #142

Closed
chrisgilmerproj opened this issue Oct 20, 2016 · 2 comments
Closed

CORS causes PutMethod operation: Method already exists ... #142

chrisgilmerproj opened this issue Oct 20, 2016 · 2 comments

Comments

@chrisgilmerproj
Copy link

chrisgilmerproj commented Oct 20, 2016

When I use the new CORS settings I get an error. Here is my method:

@app.route('/',             
           methods=['OPTIONS', 'HEAD', 'GET', 'POST'],
           cors=True)
def do_something():  

Gives me this error when I deploy:

. env/bin/activate; chalice deploy --no-autogen-policy
Updating lambda function...
Regen deployment package...
Sending changes to lambda.
Lambda deploy done.
API Gateway rest API already found.
Deleting root resource id
Done deleting existing resources.
Traceback (most recent call last):
  File "/home/chrisgilmerproj/myapp/api/env/bin/chalice", line 11, in <module>
    load_entry_point('chalice==0.3.0', 'console_scripts', 'chalice')()
  File "/home/chrisgilmerproj/myapp/api/env/local/lib/python2.7/site-packages/chalice/cli/__init__.py", line 265, in main
    return cli(obj={})
  File "/home/chrisgilmerproj/myapp/api/env/local/lib/python2.7/site-packages/click/core.py", line 716, in __call__
    return self.main(*args, **kwargs)
  File "/home/chrisgilmerproj/myapp/api/env/local/lib/python2.7/site-packages/click/core.py", line 696, in main
    rv = self.invoke(ctx)
  File "/home/chrisgilmerproj/myapp/api/env/local/lib/python2.7/site-packages/click/core.py", line 1060, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/chrisgilmerproj/myapp/api/env/local/lib/python2.7/site-packages/click/core.py", line 889, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/chrisgilmerproj/myapp/api/env/local/lib/python2.7/site-packages/click/core.py", line 534, in invoke
    return callback(*args, **kwargs)
  File "/home/chrisgilmerproj/myapp/api/env/local/lib/python2.7/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/chrisgilmerproj/myapp/api/env/local/lib/python2.7/site-packages/chalice/cli/__init__.py", line 175, in deploy
    d.deploy(config)
  File "/home/chrisgilmerproj/myapp/api/env/local/lib/python2.7/site-packages/chalice/deployer.py", line 246, in deploy
    config)
  File "/home/chrisgilmerproj/myapp/api/env/local/lib/python2.7/site-packages/chalice/deployer.py", line 682, in deploy
    return self._create_resources_for_api(config, rest_api_id)
  File "/home/chrisgilmerproj/myapp/api/env/local/lib/python2.7/site-packages/chalice/deployer.py", line 738, in _create_resources_for_api
    route_builder.build_resources(url_trie)
  File "/home/chrisgilmerproj/myapp/api/env/local/lib/python2.7/site-packages/chalice/deployer.py", line 298, in build_resources
    current['resource_id'], current['route_entry'].methods)
  File "/home/chrisgilmerproj/myapp/api/env/local/lib/python2.7/site-packages/chalice/deployer.py", line 916, in create_preflight_method
    authorizationType='NONE',
  File "/home/chrisgilmerproj/myapp/api/env/local/lib/python2.7/site-packages/botocore/client.py", line 159, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/home/chrisgilmerproj/myapp/api/env/local/lib/python2.7/site-packages/botocore/client.py", line 494, in _make_api_call
    raise ClientError(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (ConflictException) when calling the PutMethod operation: Method already exists for this resourcemyapp

When I turn off CORS like this:

@app.route('/',             
           methods=['OPTIONS', 'HEAD', 'GET', 'POST'],
           cors=False)
def do_something():  

It totally works.

I just wanted to report this in case you knew of an easy fix. I'll provide more info if it will help.

@jamesls
Copy link
Member

jamesls commented Oct 21, 2016

This is because of the OPTIONS method you have called out in your methods=[...]. When cors=True, we automatically inject an OPTIONS method to support preflight requests. This will cause the error because we'll also try to create another OPTIONS method the points to your view function.

If you don't need the OPTIONS method, the simplest fix would be to remove it from your methods=[].

That error message should be better though.

jamesls added a commit to jamesls/chalice that referenced this issue Oct 24, 2016
You'll get an error message from API gateway about a conflict
exception which isn't that helpful.  By adding it to the
validation step here, we get a much clearer error message:

```
ValueError: Route entry cannot have both cors=True and
methods=['OPTIONS', ...] configured.  When CORS is enabled, an OPTIONS
method is automatically added for you.  Please remove 'OPTIONS' from the
list of configured HTTP methods for: /badview
```

Closes aws#142.
@chrisgilmerproj
Copy link
Author

Thank you for fixing this!

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

2 participants