Giter Club home page Giter Club logo

Comments (6)

dougblack avatar dougblack commented on September 24, 2024

I agree. I'll look into this. No guarantees on how soon it will be added.

from flask-restful.

versae avatar versae commented on September 24, 2024

I don't know if this is already implemented, so I came up with this solution

from functools import wraps

def cors(func, allow_origin=None, allow_headers=None, max_age=None):
    if not allow_origin:
        allow_origin = "*"
    if not allow_headers:
        allow_headers = "content-type, accept"
    if not max_age:
        max_age = 60

    @wraps(func)
    def wrapper(*args, **kwargs):
        response = func(*args, **kwargs)
        cors_headers = {
            "Access-Control-Allow-Origin": allow_origin,
            "Access-Control-Allow-Methods": func.__name__.upper(),
            "Access-Control-Allow-Headers": allow_headers,
            "Access-Control-Max-Age": max_age,
        }
        if isinstance(response, tuple):
            if len(response) == 3:
                headers = response[-1]
            else:
                headers = {}
            headers.update(cors_headers)
            return (response[0], response[1], headers)
        else:
            return response, 200, cors_headers
    return wrapper

So you have to define your own Resource to inheritance from

class Resource(restful.Resource):
    method_decorators = [cors]


class YourResource(Resource):

    def get(self):
        return {}, 200

Or just add the decorator to your desired method

class YourResource(restful.Resource):

    @cors
    def get(self):
        return {}, 200

from flask-restful.

dougblack avatar dougblack commented on September 24, 2024

Nice workaround!

We're going to add built in CORS support in Flask-RESTful. We're deciding on what the API should look like over at #122. Would love your input.

from flask-restful.

versae avatar versae commented on September 24, 2024

Nice! I'll take a look

from flask-restful.

AlJohri avatar AlJohri commented on September 24, 2024

The pull request for CORS support has been merged. I believe this issue can be closed.

from flask-restful.

versae avatar versae commented on September 24, 2024

I agree. I will leave here, just for reference, some code changes that I implemented to support preflight requests.

from flask-restful.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.