onRequest method

  1. @override
Future onRequest (RequestOptions options)

The callback will be executed before the request is initiated.

If you want to resolve the request with some custom data, you can return a Response object or return dio.resolve. If you want to reject the request with a error message, you can return a DioError object or return dio.reject . If you want to continue the request, return the Options object.

 Future onRequest(RequestOptions options) => dio.resolve('fake data');
 ...
 print(response.data) // 'fake data';

Implementation

@override
Future onRequest(RequestOptions options) async {
  //* ommit claim and verify path
  // TODO: cnsider refactoring
  if (!options.path.contains('/auth/claim') &&
      !options.path.contains('/auth/verify')) {
    String _accessToken = (await _authApi.getAuthTokenFromCache())?.token;

    options.headers.addAll({
      'Content-type': 'application/json',
      HttpHeaders.authorizationHeader: 'Bearer $_accessToken'
    });
  }

  return options;
}