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) {
  String requestStr = "\n\n==================== Request ====================\n"
      "- URL: ${options.baseUrl + options.path}\n"
      "- METHOD: ${options.method}\n"
      "- HEADER:\n${options.headers.mapToString()}\n"
      "- QUERY: ${Transformer.urlEncodeQueryMap(options.queryParameters)}\n";

  final data = options.data;
  if (data != null) {
    if (data is Map) {
      requestStr += "- BODY:\n${data.mapToString()}\n";
    } else if (data is FormData) {
      final formDataMap = {}
        ..addEntries(data.fields)
        ..addEntries(data.files);
      requestStr += "- BODY:\n${formDataMap.mapToString()}\n";
    } else {
      requestStr += "- BODY:\n${data.toString()}\n";
    }
  }
  handler.next(options);
}