简体   繁体   中英

How to send FormData with Dio?

Hello I am sending a FormData using Dio but when this is sent, the website returns an error

    var formData = FormData.fromMap({
      "ctl00\$ScriptManager1": "ctl00\$UpdatePanel1",
      "__EVENTTARGET" :"ctl00\$cphPage\$productsControl",
      "__EVENTARGUMENT" : "",
      "__LASTFOCUS" : "",
      "PageLoadedHiddenTxtBox" : "Set",
      "Language" : "es-MX",
      "CurrentLanguage" : "es-MX",
      "Currency" : "",
      "__VIEWSTATE" :"/sadasd.....",
      "__VIEWSTATEGENERATOR" : "C0E71A90",
      "ctl00\$txtSearch" : "",
      "ctl00\$cphPage\$productsControl\$TopTools\$cbxSortType" : "",
      "ctl00\$cphPage\$productsControl\$TopTools\$cbxPageSize" : "-1",
      "ctl00\$taxes\$listCountries" : "54",
      "__ASYNCPOST" : "true",
      " " : ""
    });
var dio = Dio();
    dio.interceptors.add(InterceptorsWrapper(onRequest: (RequestOptions options) async {
      var customHeaders = {
        'content-type': 'text/html; charset=utf-8',
        'application':'x-www-form-urlencoded',
        'Cookie': 'uid=rBQBc2CisZdxWU5XBBmMAg==; ASP.NET_SessionId=tedc4xrih1hk1ykbdzlakuvb; ASPNET_Session=client=BLUR; ShopMSAuth=0102A9A3B88FC919D908FEA90B7DF1D119D908000442004C005500520000012F00FF'
        // other headers
      };
      options.headers.addAll(customHeaders);

      return options;
    }));
    Response response = await dio.post(url, data: formData, options: Options(
        contentType: 'text/html; charset=utf-8',
        followRedirects: true
    ));

    print(response.data);

When I check the requests by the browser, the requests at the end have as parameters:

__ASYNCPOST :true
 :""

Will the form be fine like this?

you can follow this path

var formData = FormData.fromMap({
 'name': 'wendux',
 'age': 25,
 'file': await MultipartFile.fromFile('./text.txt',filename: 'upload.txt')
});
response = await dio.post('/info', data: formData);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM