postMultipart static method
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 dio = Dio(BaseOptions(connectTimeout: const Duration(seconds: 10), sendTimeout: const Duration(seconds: 90), receiveTimeout: const Duration(seconds: 15)));
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) {
if (e.response != null) {
throw HttpUtil.makeTPException(e.response);
}
throw TPException()..setCode(-1)..setMessage(e.message ?? e.type.name);
} catch(e){
rethrow;
}
}