toCurl method

String toCurl({
  1. required BodyFormat bodyFormat,
})

Implementation

String toCurl({required BodyFormat bodyFormat}) {
  final buffer = StringBuffer(
    "curl \\\n"
    "  -X ${method.toUpperCase()} \\\n"
    "  '${url.toString()}'",
  );

  for (final entry in headers.entries) {
    buffer.write(" \\\n  -H '${entry.key}: ${entry.value}'");
  }

  if (this is Request) {
    switch (bodyFormat) {
      case BodyFormat.ascii:
        final body = (this as Request).body;
        if (body.isNotEmpty) {
          buffer
              .write(" \\\n  --data-ascii '${body.replaceAll("'", "\\'")}'");
        }
      case BodyFormat.binary:
        final bodyBytes = (this as Request).bodyBytes;
        if (bodyBytes.isNotEmpty) {
          final base64Body = base64Encode(bodyBytes);
          buffer.write(
            " \\\n  --data-binary '@<(echo ${base64Body} | base64 --decode)'",
          );
        }
    }
  }

  return buffer.toString();
}