1

I'm doing a web based project using flutter and flask. when I tried to use API to communicate between flutter and flask(note : I'm new to dealing with API's).

I got XML Http Request error, I tried all the solutions available but still no use and later found that installing flask-CORS module in flask can help resolve the issue. while importing flask-CORS module I faced with the above error.

Flask-CORS path and error

Flutter API code

James Z
  • 12,209
  • 10
  • 24
  • 44
kalyan
  • 31
  • 4
  • 1
    Please don't post pictures of code / errors / other text. Replace them with the original text. – James Z May 08 '22 at 15:50
  • In first image, we have only KeyError message. You are trying to fetch 'firstname' from 'req' dictionary. But that dictionary doesnot have that key. You need to fix that issue https://stackoverflow.com/a/10116540/3211801 – Nandha May 08 '22 at 16:00
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 08 '22 at 18:25
  • @Nandha , the Key Error msg is not the issue, I want to know to how to resolve the flask-CORS module error in my code. The first pic is to show that the flask-CORS has installed successfully in the correct directory of my virtual env – kalyan May 09 '22 at 03:13
  • Can you post the python flask app code as text? – Nandha May 09 '22 at 03:21
  • @Nandha sure... – kalyan May 09 '22 at 07:33

2 Answers2

2
from flask import Flask,request,jsonify
from flask_cors import CORS,cross_origin


app = Flask(__name__)
CORS(app)
app.config['ENV'] = "development"


@app.route('/auth',methods=["POST"])
@cross_origin()
def login():
    res=request.get_json()
    em=res["email"]
    pwd=res["password"]
    print(em+"\t"+pwd)
    return "success"

if __name__ == "__main__":
    app.run(debug=True)
kalyan
  • 31
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 09 '22 at 18:49
0

https://flask-cors.readthedocs.io/en/latest/

Allow access to all apis that starts with /api from all origins. cors = CORS(app, resources={r"/api/*": {"origins": "*"}})

or if you are testing from local try

cors = CORS(app, resources={r"/api/*": {"origins": ["http://localhost:5000/*"]}})

    from flask import Flask,request,jsonify
    from flask_cors import CORS,cross_origin
    
    
    app = Flask(__name__)
    cors = CORS(app, resources={r"/*": {"origins": "*"}})
    app.config['ENV'] = "development"
    
    
    @app.route('/auth',methods=["POST"])
    @cross_origin()
    def login():
        res=request.get_json()
        em=res["email"]
        pwd=res["password"]
        print(em+"\t"+pwd)
        return "success"
    
    if __name__ == "__main__":
        app.run(debug=True)
Nandha
  • 752
  • 1
  • 12
  • 37
  • Is there any version dependency of python for flask-CORS to work properly ?....because I'm using python version 3.9.7. – kalyan May 10 '22 at 10:29
  • Try after removing this decorator @cross_origin() Also can you give your OS information? is it ubuntu or windows or mac? – Nandha May 10 '22 at 18:29