dio 3.0发送多部分表单数据请求,响应代码为400

问题描述


     String filePath = file.path;
    var fileName = filePath.substring(filePath.lastIndexOf("/") + 1,filePath.length);

    multipartfile multipartfile = await multipartfile.fromFile(filePath,filename: fileName);

    FormData formData = FormData();
    formData.fields
      ..add(MapEntry("name",fileName))
      ..add(MapEntry("key","res/${aliyunCryp.filename}"))
      ..add(MapEntry("policy",aliyunCryp.policyBase64))
      ..add(MapEntry("OSSAccessKeyId",aliyunCryp.accessId))
      ..add(MapEntry("success_action_status",200.toString()))
      ..add(MapEntry("callback",aliyunCryp.callback))
      ..add(MapEntry("signature",aliyunCryp.signature))
      ..add(MapEntry("file","aliyunCryp.signature"));
    formData.files.add(MapEntry("file",multipartfile));

    dio dio = dio();
    dio.interceptors.add(LogInterceptor(requestBody: true,responseBody: true));
    (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) {
      client.badCertificateCallback = (X509Certificate cert,String host,int port) {
        Logger.print(cert);
        //暂时不校验证书
        return true;
      };
    };
    dio.options.headers = {"content-type": "multipart/form-data; boundary= ${formData.boundary}"};
    dio.post("https://tope-test-baie-bj.oss-cn-beijing.aliyuncs.com/",data: formData).then((value) {
      Logger.print("--------log---response--------$value----");
    }).catchError((onError) {
      Logger.print("--------log---response---error-----$onError----");
    });

响应错误日志:

I/Flutter (32443): Response Text:
I/Flutter (32443): <?xml version="1.0" encoding="UTF-8"?>
I/Flutter (32443): <Error>
I/Flutter (32443):   <Code>MalformedPOSTRequest</Code>
I/Flutter (32443):   <Message>The body of your POST request is not well-formed multipart/form-data</Message>
I/Flutter (32443):   <RequestId>5F60B2EB1810433430A77275</RequestId>
I/Flutter (32443):   <HostId>tope-test-baie-bj.oss-cn-beijing.aliyuncs.com</HostId>
I/Flutter (32443): </Error>

我找不到formdata问题。 我找不到formdata问题。 我找不到formdata问题。 我找不到formdata问题。 我找不到formdata问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)