How to send FormData with Dio?

Issue

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?

Solution

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);

Answered By – Ardeshir ojan

Answer Checked By – Cary Denson (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.