onRequest method

  1. @override
void onRequest(
  1. RequestOptions options,
  2. RequestInterceptorHandler handler
)

Logs outgoing request details unless options.extra['logRequest'] is false.

Emits the HTTP method, URI, query parameters, headers, and body at 'debug' level, then forwards the request to the next handler.

Implementation

@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
  if (options.extra['logRequest'] == false) {
    return super.onRequest(options, handler);
  }

  final buffer = StringBuffer();
  buffer.writeln('REQUEST');
  buffer.writeln('${options.method} ${options.uri}');

  if (options.queryParameters.isNotEmpty) {
    buffer.writeln('Query: ${options.queryParameters}');
  }

  if (options.headers.isNotEmpty) {
    buffer.writeln('Headers: ${options.headers}');
  }

  if (options.data != null) {
    buffer.writeln('Body: ${options.data}');
  }

  onLog(
    message: buffer.toString(),
    level: 'debug',
    context: 'Lucky',
  );

  super.onRequest(options, handler);
}