corydolphin / flask-cors

Cross Origin Resource Sharing ( CORS ) support for Flask
https://flask-cors.corydolphin.com/
MIT License
873 stars 140 forks source link

Barebones setup doesn't work #269

Closed knlshh closed 4 years ago

knlshh commented 4 years ago

My server code looks like (running on localhost:5000):

import os

from flask import Flask
from flask_graphql import GraphQLView
from flask_cors import CORS

from .graphql import schema

app = Flask(__name__)
CORS(app)

@app.route("/")
def home():
    return "Hello World"

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

I'm making the follow request from my client (running on localhost:3000):

fetch("http://localhost:5000/", {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
    },
    body: JSON.stringify({
      query: operation.text,
      variables,
    }),
  }).then((response) => {
    return response.json();
  });

I get the following error in the dev console on Chrome:

POST http://localhost:5000/ 405 (METHOD NOT ALLOWED)

Is the /options route something that I need to implement for my Flask app? Looks like the browser makes a preflight request:

[https://ibb.co/vsqB23x](Preflight request) [https://ibb.co/j60Kb3f](Post request)

knlshh commented 4 years ago

Working as expected after mentioning a method parameter on the app.route decorator