send method
Sends the HTTP request and returns the raw response.
This method builds the appropriate request type (regular or multipart) and sends it through the configured middleware chain.
Returns a Future that resolves to the HTTP StreamedResponse.
Throws various exceptions if the request fails to send.
Implementation
Future<http.StreamedResponse> send() async {
final client = HttpClient(http.Client());
try {
http.BaseRequest request;
if (_isMultipart) {
final multipartRequest = http.MultipartRequest(_method, _url);
multipartRequest.headers.addAll(_headers);
if (_body != null) {
final bodyMap = _body as Map<String, Object>;
for (final entry in bodyMap.entries) {
if (entry.value is http.MultipartFile) {
multipartRequest.files.add(entry.value as http.MultipartFile);
} else if (entry.value is String) {
multipartRequest.fields[entry.key] = entry.value as String;
} else {
multipartRequest.fields[entry.key] = entry.value.toString();
}
}
}
request = multipartRequest;
} else {
final regularRequest = http.Request(_method, _url);
regularRequest.headers.addAll(_headers);
if (_body != null) {
if (_body is String) {
regularRequest.body = _body;
} else if (_body is List) {
regularRequest.bodyBytes = _body.cast<int>();
} else if (_body is Map) {
regularRequest.bodyFields = _body.cast<String, String>();
} else {
throw ArgumentError('Invalid request body "$_body".');
}
}
request = regularRequest;
}
return client.use(_middlewares).send(request);
} finally {
client.close();
}
}