Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Add CORS support for python as well as ability to upload files. #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vallard
Copy link

@vallard vallard commented Jan 23, 2019

My application in python needed to support CORS. I added this. In addition I needed to be able to upload files with HTML forms. Adding the files to the event allows this to happen to make this a more useful runtime.

Copy link
Contributor

@andresmgot andresmgot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @vallard,

Thank you for the PR! I have sent a couple of comments, please take a look.

@@ -59,6 +73,7 @@ def handler():
data = req.json
event = {
'data': data,
'files': req.files, # allow file uploads
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency with the rest of runtimes we should not modify the event structure (which is documented here. If you want to access the files helper (which is only present in python) you can still access it trough event.extensions.request.files.


@app.route('/<:re:.*>', method=['GET', 'POST', 'PATCH', 'DELETE'])
if bottle.request.method != 'OPTIONS':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to do the opposite, return CORS headers if the method is OPTIONS. We can mimic what is currently being done in the nodejs runtime.

Something like (untested code, please test this):

def enable_cors(req, fn):
    def _enable_cors(*args, **kwargs):
        # set CORS headers
        bottle.response.headers['Access-Control-Allow-Origin'] = '*'

        if bottle.request.method != 'OPTIONS':
            bottle.response.headers['Access-Control-Allow-Methods'] = req.get_header('access-control-request-method')
            bottle.response.headers['Access-Control-Allow-Headers'] = req.get_header('access-control-request-headers')
        else:
            # actual request; reply with the actual response
            return fn(*args, **kwargs)

    return _enable_cors

Note that we are allowing any method and header from the request, that way we avoid having to chose the base image if there is a new method/header that we want to allow.

@meseta
Copy link

meseta commented Mar 3, 2020

it is already possible to deal with files uploads using bottle's request instance:

from bottle import request

def your_endpoint(*args):

  uploaded = request.files.get("data")
  file_contents = uploaded.file.read()
  ...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants