Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flask CORS not functioning
#1
Hello,

I am attempting to work on a backend server for a front end that will compute data.

To test this, I want to forward between ports. On the front end, I have the following code:

var myHeaders = new Headers();
         myHeaders.append("Content-Type", "application/json");
         myHeaders.append("Cross-Origin-Resource-Policy", "*")

         var raw = JSON.stringify({
            dataType: "stl"
         });

         console.log(raw)

         var requestOptions = {
            method: "POST",
            headers: myHeaders,
            mode: "cors",
            body: raw,
            redirect: "follow"
         };

         fetch("http://localhost:5000/", requestOptions)
            .then((response) => response.text())
            .then((result) => console.log(result))
            .catch((error) => console.log("error", error));
[/python]
And On the backend, I used an example flask server:

from flask import Flask, request
from urllib.request import urlopen
app = Flask(__name__)

@app.route('/', methods=['POST'])
def convert():
    print("Received Request")
    print(request.json())
    return "Success"


if __name__ == "__main__":
    app.run(debug=True)
However, I receive the following error on the fetch:
Access to fetch at 'http://localhost:5000/' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
And If I put it on no-cors:

POST http://localhost:5000/ net::ERR_ABORTED 405 (METHOD NOT ALLOWED)
Reply
#2
Hello,

I am attempting to use Flask, but I am unable to configure CORS to allow for requests from other ports.

My current Python server:

from flask import Flask, request
from urllib.request import urlopen
from flask_cors import CORS, cross_origin
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})
app.config['CORS_HEADERS'] = 'Content-Type'

@app.route('/', methods=['POST'])
@cross_origin()
def convert():
    print("Received Request")
    print(request.json())
    return "Success"


if __name__ == "__main__":
    app.run()
Despite this, any requests made to this localhost, from another port, with the following headers:

"method": "POST",
            "Access-Control-Allow-Origin": "*",
            "headers": {
               "Content-Type": "application/json"
            },
Results in the following error:

Error:
Access to fetch at 'http://localhost:5000/' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Allowing CORS in Django app with a React frontend Dexty 0 3,225 Dec-22-2021, 11:38 PM
Last Post: Dexty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020