onRequest method

  1. @override
Future onRequest (
  1. RequestOptions options
)
override

The callback will be executed before the request is initiated.

If you want to resolve the request with some custom data, you can return a Response object or return dio.resolve. If you want to reject the request with a error message, you can return a DioError object or return dio.reject . If you want to continue the request, return the Options object.

 Future onRequest(RequestOptions options) => dio.resolve('fake data');
 ...
 print(response.data) // 'fake data';

Implementation

@override
Future onRequest(RequestOptions options) async {
  logPrint('*** Request ***');
  _printKV('uri', options.uri);

  if (request) {
    _printKV('method', options.method);
    _printKV('responseType', options.responseType?.toString());
    _printKV('followRedirects', options.followRedirects);
    _printKV('connectTimeout', options.connectTimeout);
    _printKV('receiveTimeout', options.receiveTimeout);
    _printKV('extra', options.extra);
  }
  if (requestHeader) {
    logPrint('headers:');
    options.headers.forEach((key, v) => _printKV(' $key', v));
  }
  if (requestBody) {
    logPrint('data:');
    _printAll(options.data);
  }
  logPrint('');
}