onRequest method

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

Called when the request is about to be sent.

Implementation

@override
void onRequest(
    RequestOptions options, RequestInterceptorHandler handler) async {
  final cookieManager = CookieManager.instance();
  // Get all cookies associated with the request's domain.
  final cookies =
      await cookieManager.getCookies(url: WebUri.uri(options.uri));
  // Format the cookies into a single `Cookie` header string.
  final cookieHeader =
      cookies.map((cookie) => '${cookie.name}=${cookie.value}').join('; ');
  // Add the cookie header to the request if any cookies were found.
  if (cookieHeader.isNotEmpty) {
    options.headers['cookie'] = cookieHeader;
  }
  // Continue with the modified request.
  handler.next(options);
}