onRequest method

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

The callback will be executed before the request is initiated.

If you want to continue the request, call handler.next.

If you want to complete the request with some custom data, you can resolve a Response object with handler.resolve.

If you want to complete the request with an error message, you can reject a DioError object with handler.reject.

Implementation

@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
  getCookie(options.uri.toString()).then((cookies) {
    var cookie = _getCookies(cookies);
    if (cookie.isNotEmpty) {
      options.headers[HttpHeaders.cookieHeader] = cookie;
    }
    handler.next(options);
  }).catchError((e, stackTrace) {
    var err = DioError(requestOptions: options, error: e);
    err.stackTrace = stackTrace;
    handler.reject(err, true);
  });
}