postMultipart static method

Future<Map<String, dynamic>> postMultipart(
  1. String url,
  2. Map<String, dynamic> jsonBody,
  3. File? file
)

Implementation

static Future<Map<String, dynamic>> postMultipart(String url, Map<String, dynamic> jsonBody, File? file) async {
  var headers = {'Content-Type': "multipart/form-data"};
  var requestUrl = _mBaseUrl + url;
  if (getSessionToken() != null) { headers["Authorization"] = "Bearer ${getSessionToken()}"; }
  if (getAppId() != null) { headers["app-id"] = getAppId()!; }

  if (file != null) {
    String fileName = file.path.split('/').last;
    jsonBody['file'] = await MultipartFile.fromFile(
      file.path,
      filename: fileName,
    );
  }

  try {
    var formData = FormData.fromMap(jsonBody);
    final response = await Dio().post(requestUrl, options: Options(headers: headers), data: formData);
    if (response.statusCode! >= 200 && response.statusCode! < 300) {
      return response.data;
    }
    throw HttpUtil.makeTPException(response);
  } on DioException catch (e) {
    throw HttpUtil.makeTPException(e.response);
  } catch(e){
    rethrow;
  }
}