postAPI method
Future<BaseResponse>
postAPI(
- String url,
- String method,
- dynamic data, {
- bool fullPath = false,
- int timeout = 20,
- Map<String, String>? body,
- List<String>? files,
- List<FileByte>? realFiles,
- String paramFile = 'attachment[file][]',
})
Implementation
Future<BaseResponse> postAPI(String url, String method, data,
{bool hasHeader = true, bool fullPath = false, int timeout = 20,
Map<String, String>? body,
List<String>? files,
List<FileByte>? realFiles,
String paramFile = 'attachment[file][]'}) async {
final baseResponse = BaseResponse();
try {
final request = http.MultipartRequest(method, Uri.parse(fullPath ? url : (Constants().baseUrl + url)));
if (hasHeader) request.headers.addAll(await _getHeader());
if (body != null) request.fields.addAll(body);
if (files != null && files.isNotEmpty) {
for (String path in files)
request.files.add(_createPartFile(File(path), paramFile));
} else if (realFiles != null) {
for (FileByte file in realFiles)
request.files.add(_createPartFileByte(file, paramFile));
}
final streamResponse = await request.send().timeout(Duration(seconds: request.files.isEmpty ? timeout : Constants().timeout));
final response = await http.Response.fromStream(streamResponse);
if (showLog != null && showLog!) {
logDev.log('\n\nadvn-request url: ' + request.url.toString());
logDev.log('\nadvn-request body: $body');
logDev.log('\nadvn-response: ${response.body}');
}
baseResponse.fromJson(jsonDecode(response.body), data);
} catch (e) {
return _responseError(baseResponse, e);
}
return baseResponse;
}