使用 dart 向 Flask 发出请求会给出带有代码 200 的选项

问题描述 投票:0回答:1

我正在尝试使用 dart 向我的 Flask 后端发送发布请求,这是我在 dart 上的发布请求

void createUser({required String email, required String password}) async{
    final Map<String, String> headers = {'Content-Type': 'application/json'};

    Map<String, String> data = {
      "email": email,
      "password": password,
    };

    try {
      final response = await http.post(Uri.parse("http://localhost:5000/register"), headers: headers, body: json.encode(data));
    } on Exception catch (e) {
      rethrow;

    }
  }

这是后烧瓶

    def post(self):
        arguments = reqparse.RequestParser()

        arguments.add_argument('email', type=str, required=True, help="Email is required")
        arguments.add_argument('password', type=str, required=True, help="Password is required")

        user_data = arguments.parse_args()

        if User.find_user(user_data['email']):
            return {"error": "User already exists"}, 409

        user = User(**user_data)
        user.save_user()
        return {"message": "User created successfully"}, 201

能够像这样正常地使用curl进行发布请求

curl -H "Content-Type: application/json" -X POST -d '{"email":"[email protected]", "password":"12345678"}' http://localhost:5000/register

但是当我在调用 createUser 方法的 flutter 前端运行它时,它会在 Flask 终端上显示这一点

127.0.0.1 - - [10/Aug/2023 17:45:59] "OPTIONS /register HTTP/1.1" 200 -
flutter dart flask flask-restful dart-http
1个回答
0
投票

通过向烧瓶中添加 CORS 来修复

from flask_cors import CORS


app = Flask(__name__)
app.config.from_object(AppConfiguration)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
api = Api(app)
© www.soinside.com 2019 - 2024. All rights reserved.