Milad-Akarie / pretty_dio_logger

MIT License
220 stars 49 forks source link

[Feature Request]: Add curl logging option #18

Closed LeXuanKhanh closed 4 days ago

LeXuanKhanh commented 3 years ago

Hi, the answer in https://github.com/flutterchina/dio/issues/687 have no longer work, can you add the options which will log the additional curl of the request ?

HugoHeneault commented 2 years ago

Based on the answers on the issue you linked, this is how I managed to print curl request onError. You can do the same with other interceptors:


String cURLRepresentation(RequestOptions options) {
  List<String> components = ['\$ curl -i'];
  if (options.method.toUpperCase() == 'GET') {
    components.add('-X ${options.method}');
  }

  options.headers.forEach((k, v) {
    if (k != 'Cookie') {
      components.add('-H \"$k: $v\"');
    }
  });

  var data = json.encode(options.data);
  data = data.replaceAll('\"', '\\\"');
  components.add('-d \"$data\"');

  components.add('\"${options.uri.toString()}\"');

  return components.join('\\\n\t');
}

    _dio.interceptors.add(InterceptorsWrapper(onError: (DioError e, handler) {
      // add a breakpoint here so all errors can break
      try {
        print(cURLRepresentation(e.requestOptions));
      } catch (err) {
        print('unable to create a CURL representation of the errored');
      }

      return handler.next(e); //continue
    }));
Milad-Akarie commented 4 days ago

not in the scope of this lib