toCurlString method

String toCurlString()

Converts the Curl object to a formatted cURL command string.

Implementation

String toCurlString() {
  var cmd = 'curl ';

  // Add the request method
  if (method != 'GET' && method != 'HEAD') {
    cmd += '-X $method ';
  }
  if (method == 'HEAD') {
    cmd += '-I ';
  }

  // Add the URL
  cmd += '"${Uri.encodeFull(uri.toString())}" ';
  // Add the headers
  headers?.forEach((key, value) {
    cmd += '\\\n -H "$key: $value" ';
  });

  // Add the body
  if (data?.isNotEmpty == true) {
    cmd += "\\\n -d '$data' ";
  }
  // Add the cookie
  if (cookie?.isNotEmpty == true) {
    cmd += "\\\n -b '$cookie' ";
  }
  // Add the user
  if (user?.isNotEmpty == true) {
    cmd += "\\\n -u '$user' ";
  }
  // Add the referer
  if (referer?.isNotEmpty == true) {
    cmd += "\\\n -e '$referer' ";
  }
  // Add the user-agent
  if (userAgent?.isNotEmpty == true) {
    cmd += "\\\n -A '$userAgent' ";
  }
  // Add the form flag
  if (form) {
    for (final formEntry in formData!) {
      cmd += "\\\n -F ";
      if (formEntry.type == FormDataType.file) {
        cmd += '"${formEntry.name}=@${formEntry.value}" ';
      } else {
        cmd += '"${formEntry.name}=${formEntry.value}" ';
      }
    }
  }
  // Add the insecure flag
  if (insecure) {
    cmd += "-k ";
  }
  // Add the location flag
  if (location) {
    cmd += "-L ";
  }

  return cmd.trim();
}