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 {
  DataDomeLogger.info('Validating request ${options.uri}');

  String datadomeCookie = await getDataDomeCookie();

  if (datadomeCookie.isNotEmpty) {
    String cookies = options.headers['Cookie'] ?? "";

    // The header Cookie contains the DataDome cookie for request retry after a challenge.
    // This check ensures also that the DataDome cookie is not duplicated
    // in case of improper cookie handling.
    if (cookies.contains(RegExp(r'\bdatadome=[^;]*'))) {
      cookies =
          cookies.replaceFirst(RegExp(r'\bdatadome=[^;]*'), datadomeCookie);
    } else {
      cookies =
          cookies.isNotEmpty ? '$cookies; $datadomeCookie' : datadomeCookie;
    }
    options.headers['Cookie'] = cookies;
  }

  await fillPayload(datadomeCookie, options);

  return handler.next(options);
}