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) {
if (request) {
_printRequestHeader(options);
}
if (requestHeader) {
_printMapAsTable(options.queryParameters, header: 'Query Parameters');
final requestHeaders = <String, dynamic>{};
requestHeaders.addAll(options.headers);
requestHeaders['contentType'] = options.contentType?.toString();
requestHeaders['responseType'] = options.responseType.toString();
requestHeaders['followRedirects'] = options.followRedirects;
requestHeaders['connectTimeout'] = options.connectTimeout;
requestHeaders['receiveTimeout'] = options.receiveTimeout;
_printMapAsTable(requestHeaders, header: 'Headers');
_printMapAsTable(options.extra, header: 'Extras');
}
if (requestBody && options.method != 'GET') {
final dynamic data = options.data;
if (data != null) {
if (data is Map) _printMapAsTable(options.data as Map?, header: 'Body');
if (data is FormData) {
final formDataMap = <String, dynamic>{}
..addEntries(data.fields)
..addEntries(data.files);
_printMapAsTable(formDataMap, header: 'Form data | ${data.boundary}');
} else {
_printBlock(data.toString());
}
}
}
// 创建包含请求信息的 LogEntry
logEntry = LogEntry(
requestMethod: options.method,
requestUri: options.uri,
requestHeaders: options.headers,
requestBody: options.data,
timestamp: DateTime.now(),
);
super.onRequest(options, handler);
}