Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Flask Rest API not passing preflight w/CORS

I am trying to get the simplest rest api up that I can in python and I am having trouble. I'm not sure what I am doing wrong, but I think it has something to do with CORS. This is frustrating, as I have used the flask_cors package in order to fix this and it does not appear to work.

In my main.py file i have the following

from flask import Flask
from flask_cors import CORS, cross_origin

app = Flask(__name__)
CORS(app)

import routes.login

if __name__ == '__main__':
    app.run(debug=True)

For my project I have this as my folder structure:

main.py
__init__.py
routes
  __init__.py
  login.py

And i have the following code in login.py

from flask import Flask
from flask_cors import CORS, cross_origin

from main import app
CORS(app)

@app.route('/login', methods=['POST', 'GET'])
@cross_origin()
def login(name, password):
    if request.method == 'POST':
        print('inside login POST')
    if request.method == 'GET':
        print('inside login GET')

I'm currently getting this error:

xhr.js:178 OPTIONS http://localhost:5000/login 404 (NOT FOUND)
dispatchXhrRequest @ xhr.js:178
xhrAdapter @ xhr.js:12
dispatchRequest @ dispatchRequest.js:52
:3000/pictureswapper:1 XMLHttpRequest cannot load http://localhost:5000/login.
 Response for preflight has invalid HTTP status code 404

There is some sort of CORS error, but I really don't know what's going wrong. Any ideas?

EDIT: The only place in the documentation that has anything to say about preflight is here (https://flask-cors.readthedocs.io/en/v1.3.1/index.html?highlight=preflight). If I add

@cross_origin(headers=['Content-Type']) # Send Access-Control-Allow-Headers

It doesn't break the application, but neither does it fix the error.

like image 370
Peter Weyand Avatar asked Jul 09 '17 18:07

Peter Weyand


People also ask

How do I enable CORS in python flask?

Simply add @cross_origin() below a call to Flask's @app. route(..) to allow CORS on a given route. See the full list of options in the decorator documentation.

What is CORS error in Python?

Cross-Origin Resource Sharing (CORS) errors occur when a server doesn't return the HTTP headers required by the CORS standard. To resolve a CORS error from an API Gateway REST API or HTTP API, you must reconfigure the API to meet the CORS standard.

What is preflight and XHR?

Preflight is a request the XHR object makes to ensure it's allowed to make another request.


1 Answers

Revisiting this after a few months.

One of the nasty gotchas in python/flask appears to be that the compiled code will get cached, so if you change something at the entrypoint of the app (ie main.py), and don't delete the binaries that flask export creates then every time you run flask export and recompile it may be using old code!

Make sure to delete (in atom they are the purple 1/0 files -if you have file icons enabled- labeled .pyc) these files if you are getting spooky output.

like image 91
Peter Weyand Avatar answered Sep 28 '22 08:09

Peter Weyand