How to send FormData with Dio?
Asked Answered
A

1

12

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?

Amabel answered 18/5, 2021 at 6:24 Comment(0)
F
32

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);
Flittermouse answered 18/5, 2021 at 6:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.