0

I am using Flask-Restless extension for my simple REST service. And now I want to enable CORS.

According this article http://flask-restless.readthedocs.org/en/latest/customizing.html I tried to use the following code to enable CORS:

import flask
import flask.ext.sqlalchemy
import flask.ext.restless
from flask import Flask

app = Flask(__name__)

app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:password@localhost/db'
db = flask.ext.sqlalchemy.SQLAlchemy(app)

class Cost(db.Model):
    __tablename__ = 'costs'
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.Unicode, unique = True)
    amount = db.Column(db.Float)
    date = db.Column(db.DateTime)


def add_cors_headers(response):
    response.headers['Access-Control-Allow-Origin'] = '*'
    response.headers['Access-Control-Allow-Credentials'] = 'true'
    return response

db.create_all()
manager = flask.ext.restless.APIManager(app, flask_sqlalchemy_db = db)

blueprint = manager.create_api(Cost)
blueprint.after_request(add_cors_headers)

app.run()

But blueprint object has no attribute after_request.

What did I do wrong?

Sergey
  • 5,396
  • 3
  • 26
  • 38
  • After some research I've found [Flask-CORS extension](http://flask-cors.readthedocs.org/en/latest/). So, problem has been solved. – Sergey Feb 18 '15 at 19:01

1 Answers1

0

Can you give any information on how to use the CORS module correcty. Following the instructions on the documentation hasn't solved any of my problems.

David McLean
  • 191
  • 1
  • 12
  • I import CORS module `from flask.ext.cors import CORS`, next I call `cors = CORS(app, resources={r"/api/*": {"origins": "*"}})` and add CORS headers to config `app.config['CORS_HEADERS'] = 'Content-Type'` – Sergey Feb 24 '15 at 15:30