onRequest method

  1. @override
void onRequest(
  1. String method,
  2. String url,
  3. Map<String, dynamic> headers, [
  4. String? body,
  5. String? dohProvider,
])
override

Called before the request is sent

Implementation

@override
void onRequest(String method, String url, Map<String, dynamic> headers,
    [String? body, String? dohProvider]) {
  if (!enabled) return;

  // Store timestamp for calculating response time
  _requestTimestamps[url] = DateTime.now().millisecondsSinceEpoch;

  if (request) {
    _printRequestHeader(method, url);
  }

  if (requestHeader) {
    _printMapAsTable(headers, header: 'Headers');
  }

  if (requestBody && body != null && method != 'GET' && method != 'DELETE') {
    final requestBlock = StringBuffer('╔ Body\n');
    try {
      final dynamic data = json.decode(body);
      if (data is Map) {
        requestBlock.write(_formatPrettyMap(data));
      } else if (data is List) {
        requestBlock.write('${_indent()}[\n');
        requestBlock.write(_formatList(data));
        requestBlock.write('${_indent()}]\n');
      } else {
        requestBlock.write(body);
      }
    } catch (e) {
      requestBlock.write(body);
    }
    requestBlock.write('╚ ${_repeatChar('═', maxWidth)}');
    logPrint(requestBlock.toString());
  }
}