cURLRepresentation static method

String cURLRepresentation(
  1. RequestOptions options
)

Implementation

static String cURLRepresentation(RequestOptions options) {
  List<String> components = ['curl -i'];
  if (options.method.toUpperCase() != HTTPMethod.get.name) {
    components.add('-X ${options.method}');
  }

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

  if (options.data != null) {
    // FormData can't be JSON-serialized, so keep only their fields attributes
    if (options.data is FormData) {
      options.data = Map.fromEntries((options.data as FormData).fields);
    }

    final data = const JsonEncoder.withIndent('   ')
        .convert(options.data)
        .replaceAll('"', '\\"');
    components.add('-d "$data"');
  }

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

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