onRequest method

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

Called when the request is about to be sent.

Implementation

@override
Future<void> onRequest(
  RequestOptions options,
  RequestInterceptorHandler handler,
) async {
  try {
    final cookies = await loadCookies(options);
    options.headers[HttpHeaders.cookieHeader] =
        cookies.isNotEmpty ? cookies : null;
    handler.next(options);
  } catch (e, s) {
    final exception = DioException(
      requestOptions: options,
      type: DioExceptionType.unknown,
      error: CookieManagerLoadException(error: e, stackTrace: s),
      message: 'Failed to load cookies for the request.',
    );
    handler.reject(exception, true);
  }
}