toSafeUrl method

String toSafeUrl()

Cleans and safely encodes the current string as a URL.

  • Trims whitespace.
  • Removes carriage return characters (which may cause HTTP 400 errors).

Implementation

String toSafeUrl() {
  String encodedUrl = Uri.encodeComponent(this.trim());
  // Remove carriage returns (common source of %0D)
  encodedUrl = encodedUrl.replaceAll('%0D', '');
  return Uri.decodeComponent(encodedUrl);
}