无法在dio中发送发布请求-Flutter

时间:2020-03-08 17:19:52

标签: python flutter post flask dio

  • 嗨,我正在尝试将Flutter(dio)中的发帖请求发送到我的烧瓶restful-api。
  • 我想在表单数据中提供请求正文,因为我 需要发送图像文件。
  • 我在 邮递员,它可以与我的邮递员的表格数据一起正常工作。
  • 但是当我尝试使用flutter的dio包将表单数据发布到我的烧瓶时, 在烧瓶中出现错误,如下所述。

dio代码为:

Future<List> apiRequest(int N ,File file) async {
  String url = "http://10.0.2.2:5000/";
  // HttpClient httpClient = new HttpClient();
          Post _post = Post();

  var bytes = await file.readAsBytes();
  //TODO : dio post request
  var dio = new Dio();
  MultipartFile  multipartFile = new MultipartFile.fromBytes(bytes);
  FormData formData = new FormData();
  dio.options.headers = {"content-type" :"multipart/form-dataitem"};
  formData.fields.add(MapEntry("index" , N.toString()));
  formData.files.add(MapEntry("file",multipartFile));
  print(formData);
   await dio.post(url, data: formData,options: Options(
            method: 'POST',
            responseType: ResponseType.json // or ResponseType.JSON
        )).then((response){
             //response stuffs
        });

邮递员的要求: this image consists of my postman's Post request which works fine

我的烧瓶声明使用此表单数据是:

        fromJson = request.form['index']
        Img = request.files['file']

烧瓶控制台中的错误是:

{exception: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.}

0 个答案:

没有答案