send<T> static method
Implementation
static Future<T> send<T> (final String method, final String host, final String path, [
final Map<String, dynamic>? data,
]) async {
/// http请求对象
final request = await HttpClient().openUrl(method, Uri.http(host, path));
/// 设置http协议类型为form-data,并设置字段分隔符boundary
if (method != 'get' && data != null) {
final boundary = 'abcdefghijklmnopqrstuvwxyz';
request.headers.contentType = ContentType('multipart', 'form-data', parameters: {
'boundary': boundary,
});
/// 分别将字段写入http报文
for (final key in data.keys) {
final value = data[key];
if (value is File) {
/// 写入文件字段
request.write('\n--$boundary\n');
request.write('Content-Disposition: form-data; name=$key; filename=${value.path}\n\n');
request.write('${String.fromCharCodes(value.readAsBytesSync())}\n');
} else {
/// 写入其他字段
request.write('\n--$boundary\n');
request.write('Content-Disposition: form-data; name=$key\n\n');
request.write('$value\n');
}
}
/// 写入http报文结尾
request.write('--$boundary--');
}
/// 获取服务器响应
final response = await request.close();
/// 转为utf8编码的json字符串
final json = await response.transform(Utf8Decoder()).join();
/// 转为指定类型
final res = jsonDecode(json) as T;
/// 返回
return res;
}