postJson static method
Implementation
static Future<Map<String, dynamic>> postJson(String url, Object body, {Duration? timeout}) async {
var headers = {'Content-Type': "application/json"};
var requestUrl = _mBaseUrl + url;
if (getSessionToken() != null) { headers["Authorization"] = "Bearer ${getSessionToken()}"; }
if (getAppId() != null) { headers["app-id"] = getAppId()!; }
try {
final effectiveTimeout = timeout ?? const Duration(seconds: 15);
final dio = Dio(BaseOptions(connectTimeout: const Duration(seconds: 10), receiveTimeout: effectiveTimeout));
final response = await dio.post(requestUrl,
options: Options(headers: headers, receiveDataWhenStatusError: true),
data: jsonEncode(body));
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;
}
}