Search code examples
flutterfile-uploaddio

Invalid request payload input on flutter post request


I am trying to uploading multiple files from my flutter mobile app. Here is my screenshot of the postman. This works fine on postMan. But I am getting 400 errors on the flutter mobile app. I can't find out where is the problem? enter image description here

enter image description here

Now here is the code of my flutter mobile app upload.

 if(images.length>0){
      for (int i = 0; i < images.length; i++) {
        var path = await FlutterAbsolutePath.getAbsolutePath(images[i].identifier);
        String fileName = path.split('/').last;
        var file = await MultipartFile.fromFile(path, filename:fileName);
        multipart.add(file);
      }

      FormData imageFormData = FormData.fromMap(
          {
            "feedId": value.id,
            "images": multipart,
            "userInformationsId": value.userInformationId
          });
      print(multipart.length);
      uploadFeedPicture(imageFormData);
  }

 Future<String> uploadFeedPicture(FormData _imageformData) async{

    String at = await _apiProvider.getAccessToken();
    Dio dio = new Dio();
   // dio.options.headers['accept'] = 'application/json';
  //  dio.options.headers["content-Type"] = "multipart/form-data";
    dio.options.headers["Authorization"] = "Bearer ${at}";
    dio.options.baseUrl = getBaseUrl();

    var _baseUrl = getBaseUrl();
     await dio.post('/api/feed/upload', data: _imageformData,  options: Options(
         followRedirects: false,
         validateStatus: (status) { return status < 500; }
     ), onSendProgress: (int sent, int total) {
      print("$sent $total");
    },).then((value) {
      print(value.data);

      print(value.headers);

    }
    ).catchError((error) => print(error) );




 }

And I am getting this response.

{statusCode: 400, error: Bad Request, message: Invalid request payload input}

Please Help me out where is the problem? I try with change content-type but not working.


Solution

  • After trying so many different ways I found the solution. Here it is.

    if(value.id != null){
    
        String at = await _apiProvider.getAccessToken();
        Map<String, String> headers = { "Authorization": "Bearer ${at}"};
        var uri = Uri.parse('$baseUrl/api/feed/upload');
        var  request = http.MultipartRequest('POST', uri);
        request.headers.addAll(headers);
        if(images.length>0){
          for (int i = 0; i < images.length; i++) {
            var path = await FlutterAbsolutePath.getAbsolutePath(images[i].identifier);
           // String fileName = path.split('/').last;
    
            var file = await MultipartFile.fromPath("images",path);
    
              request.files.add(file);
          }
          request..fields['feedId'] = value.id;
          request..fields['userInformationsId'] = value.userInformationId;
    
    
          var response = await request.send();
          if (response.statusCode == 200) {
            Navigator.pop(context);
            showSuccessToast("Posted succesfull");
            counter.update(0);
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => HomePage()),
            );
          }else{
            showErrorToast("Upload image failed");
          }
    
    
        }
      }