Search code examples
kotlinflutterdartretrofit2chopper

How to send file using Chopper?


In my kotlin project, I use retrofit and it works well.

suspend fun createPlan(
    context: Context?,
    name: String,
    file: File?
): ABC? {

    val fileSignImage = file?.let {
        MultipartBody.Part.createFormData(
            "image",
            it.getName(),
            RequestBody.create("image/*".toMediaTypeOrNull(), it)
        )
    }

    return RetrofitFactory.apiCall(context) {
        RetrofitFactory.makeRetrofitService().createPlan(
            name.toRequestBody("text/plain".toMediaTypeOrNull()),
            fileSignImage
        )
    }} 

RetrofitService

@Multipart
@POST("create_plan")
fun createPlan(
    @Part("name") name: RequestBody,
    @Part image: MultipartBody.Part?
): Deferred<Response<WebApiResponse.ABCs>>

If I want to use Chopper, what is the correct way?

This is what I have tried

Future<Response> createPlan(
      BuildContext context, String name,String path) async {
    Response response;
    try {
      response = await _service.createPlan(
           name,path);
      return response;
    } catch (e) {
      rethrow;
    }
  }

Service

@Post(path: "create_plan")
@multipart
Future<Response> createPlan(
@Field('name') String name,@PartFile('image') String imagePath);

How can I convert the imagePath to file so I can pass it as file to server using Chopper?

Anyone?


Solution

  • I was managed to upload file using http instead of Chopper.

       Future<http.Response> createPlan(String name, String path) async {
            var request = http.MultipartRequest(
                "POST",
                Uri.parse(
                    "http://xxx"));
    
            request.fields['name'] = name;
            request.files.add(await http.MultipartFile.fromPath(
              'image',
              path,
            ));
    
            try {
              var streamedResponse = await request.send();
              var response = http.Response.fromStream(streamedResponse);
              return response;
            } catch (e) {
             rethrow;
            }
          }