trevorwang / retrofit.dart

retrofit.dart is an dio client generator using source_gen and inspired by Chopper and Retrofit.
https://mings.in/retrofit.dart/
MIT License
1.06k stars 241 forks source link

Add support for sending String as a file using MultipartFile.fromString #684

Open mihir-kandoi opened 1 month ago

mihir-kandoi commented 1 month ago

To send a JSON object as a String along with some Files, I need to explicitly change the generated .g.dart file from

_data.fields.add(MapEntry(
      'userDTO',
      userDTO,
    ));

to

_data.files.add(MapEntry(
      'userDTO',
      MultipartFile.fromString(
        userDTO,
        filename: 'userDTO',
        contentType: MediaType.parse('application/json'),
      ),
    ));

for the API call to work. Without it I always receive a 415 error saying that application/octet-stream is not supported as content type for the JSON parameter. I tried setting the content type to application/json in the Part annotation of the parameter to no avail. I also tried manually sending a MultipartFile object to the request but that didn't work as well.

I would like a provision in the package which allows us to send a String as a MultipartFile in the files fields of the FormData being generated by the generator.

Any alternative solution to fix this problem is also fine. Thanks.