onRequest method
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,
) {
final metadataAuthInfo = getAuthInfo(options, 'basic');
for (final info in metadataAuthInfo) {
final authName = info['name'] as String?;
final basicAuthInfo = authInfo[authName!];
if (basicAuthInfo != null) {
final basicAuth = 'Basic ' +
base64Encode(utf8
.encode('${basicAuthInfo.username}:${basicAuthInfo.password}'));
options.headers['Authorization'] = basicAuth;
break;
}
}
return super.onRequest(options, handler);
}