Flutter Multipart Request with headers and other fields

Viewed 1467

I want to send a Multipart request to my server like following :

{
  "file" : some_file.jpg,
  "room" : {
             "roomName" : "Room Name"
           }
}

I have certain headers

{
  "authorisation" : "Basic header"
  "screenInfo" : "screenInfo",
  "operation" : "operation",
  "userId" : "integer",
}

I have tried http.Multipart request as well as Dio package, but I am unable to send the request properly

What I have Used :

  1. http.MultipartRequest

    final length = _mWebRequest.getRequestFiles()["file"].lengthSync();
    print("Length : $length");
     var request = http.MultipartRequest('POST', Uri.parse(urlString))
       ..headers.addAll(_mWebRequest.getHeaders())
       ..fields.addAll(_mWebRequest.getRequestParam())
       ..files.add(
         http.MultipartFile.fromBytes('file',
             _mWebRequest.getRequestFiles()["file"].readAsBytesSync(),
             filename: _mWebRequest
                 .getRequestFiles()["file"]
                 .path
                 .split("/")
                 .last),
       );
    
  2. Dio package

    Dio dio = new Dio();
     var formData = FormData.fromMap({
       "room": {"roomName": "Custom Room 1", "isDefault": "false"},
       "file": await MultipartFile.fromFile(
           _mWebRequest.getRequestFiles()["file"].path,
           filename:
               _mWebRequest.getRequestFiles()["file"].path.split("/").last)
     });
     dio.options.headers.addAll(_mWebRequest.getHeaders());
     var response = await dio.post(urlString, data: formData);
    

Can someone help me out ?

1 Answers

Late but , you have a typo in "authorisation" is with Z, Authorization

Related