How to learn about http_interceptor using http package in multipart request

394 Views Asked by At

I am trying to implement http_interceptor for refreshing access token on expiry and retry http request. I had searched over internet but I had not found any useful links to work following is my code for multipart request in which I want to implement http_interceptor.

Future<void> postImage(BuildContext context, String imageType) async {
    var data = {"imagetype": imageType, "filename": imageType};
    Map<String, String> obj = {"attributes": json.encode(data).toString()};
    var flutterFunctions =
        Provider.of<FlutterFunctions>(context, listen: false);
    final url = Ninecabsapi().urlHost + Ninecabsapi().getvehicle;
    try {
      loading();
      var response = await http.MultipartRequest("POST", Uri.parse(url))
        ..files.add(await http.MultipartFile.fromPath(
            "imagefile", flutterFunctions.imageFile!.path,
            contentType: MediaType("image", "jpg")))
        ..headers['Authorization'] = token!
        ..fields.addAll(obj);
      loading();
      notifyListeners();
    } catch (e) {
      print(e);
    }
  }
2

There are 2 best solutions below

0
On

Support for intercepting MultiPartForm data is available in https://pub.dev/packages/http_interceptor/versions/2.0.0-beta.6 Try using it.

1
On

Suggest you to use Dio library to more easy handle the HTTP Request https://pub.dev/packages/dio