getCurl method

String getCurl(
  1. CurlModel request, {
  2. bool redactHeaders = true,
})

Generates a cURL command string from a CurlModel request.

This method constructs a cURL command using the HTTP method, headers, body, and URL from the provided CurlModel request.

Returns a String representing the cURL command.

Implementation

String getCurl(CurlModel request, {bool redactHeaders = true}) {
  final StringBuffer stringBuilder = StringBuffer()..write('curl -X ${request.method}');

  request.headers.forEach((String key, dynamic value) {
    if (key.toLowerCase() != 'content-length') {
      final String display = _formatHeaderValue(key, value, redactHeaders);
      final String escaped = escapeHeaderValueForDoubleQuotes(display);
      stringBuilder.write(' -H "$key: $escaped"');
    }
  });

  final String? bodyString = _curlBodyString(request.body);
  if (bodyString != null && bodyString.isNotEmpty) {
    final String esc = escapeForSingleQuotedShell(bodyString);
    stringBuilder.write(" -d '$esc'");
  }

  stringBuilder.write(' "${request.url}"');

  return stringBuilder.toString();
}