send method
Sends an HTTP request and asynchronously returns the response.
Implementers should call BaseRequest.finalize to get the body of the
request as a ByteStream. They shouldn't make any assumptions about the
state of the stream; it could have data written to it asynchronously at a
later point, or it could already be closed when it's returned. Any
internal HTTP errors should be wrapped as ClientExceptions.
Implementation
@override
Future<http.StreamedResponse> send(http.BaseRequest request) async {
// Only log in Debug mode to save production performance
if (kDebugMode) {
debugPrint('========== REQUEST ==========');
debugPrint('METHOD : ${request.method}');
debugPrint('URL : ${request.url}');
debugPrint('HEADERS: ${request.headers}');
}
final response = await _client.send(request);
if (kDebugMode) {
debugPrint('========== RESPONSE ==========');
debugPrint('STATUS : ${response.statusCode}');
}
final bytes = await response.stream.toBytes();
if (kDebugMode) {
try {
final rawString = utf8.decode(bytes);
// Attempt to pretty print if it's a JSON response
final jsonObject = json.decode(rawString);
final prettyJson = const JsonEncoder.withIndent(' ').convert(jsonObject);
debugPrint('BODY :\n$prettyJson');
} catch (_) {
// Fallback if payload isn't clear text or valid JSON
debugPrint('BODY : ${String.fromCharCodes(bytes)}');
}
}
return http.StreamedResponse(
Stream.value(bytes),
response.statusCode,
contentLength: response.contentLength,
request: response.request,
headers: response.headers,
isRedirect: response.isRedirect,
persistentConnection: response.persistentConnection,
reasonPhrase: response.reasonPhrase,
);
}