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 {
  try {
    final token = await getToken?.call() ?? '';
    if (token.isNotEmpty) {
      options.headers['Authorization'] = 'Bearer $token';
    }

    debugPrint(
      "✅ REQUEST [${options.method}] => ${options.uri} \n"
      "HEADERS: ${options.headers} \n"
      "QUERY: ${options.queryParameters} \n"
      "BODY: ${options.data}",
    );
  } catch (e) {
    debugPrint("❌ Failed to attach token: $e");
  }

  handler.next(options);
}