onRequest method

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

Called when the request is about to be sent.

Implementation

@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
  cookieJar.loadForRequest(options.uri).then((cookies) {
    final previousCookies =
        options.headers[HttpHeaders.cookieHeader] as String?;
    final newCookies = getCookies([
      ...?previousCookies
          ?.split(';')
          .where((e) => e.isNotEmpty)
          .map((c) => Cookie.fromSetCookieValue(c)),
      ...cookies,
    ]);
    options.headers[HttpHeaders.cookieHeader] =
        newCookies.isNotEmpty ? newCookies : null;
    handler.next(options);
  }).catchError((dynamic e, StackTrace s) {
    final err = DioException(
      requestOptions: options,
      error: e,
      stackTrace: s,
    );
    handler.reject(err, true);
  });
}