urlencode()

The urlencode() function percent-encodes a string and returns it in a format that can be safely included in a URL.

Syntax

urlencode(STR[, CHARSET])

Parameters

STR
The string field or value to encode. Only string types are accepted.
CHARSET
(Optional) The character set to use for encoding. The default is "utf-8". Use a name registered in the IANA Charset Registry.

Description

The urlencode() function percent-encodes STR and returns the result. It uses Java's URLEncoder.encode() method.

  • If STR is null, the function returns null.
  • If STR is not a string type, the function returns null.
  • If an encoding error occurs, the function returns null.
  • If an invalid character set name is specified for CHARSET, error code 90850 is raised.

Error codes

Error codeDescription
90850An invalid character set name was specified for CHARSET.

Usage examples

To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.

  1. URL-encode a URI

    table limit=5 WEB_APACHE_SAMPLE | eval result = urlencode(uri) | fields uri, result
    
  2. URL-encode a Korean string

    json "{'val': '로그분석'}" | eval result = urlencode(val)
    | # result: "%EB%A1%9C%EA%B7%B8%EB%B6%84%EC%84%9D"
    
  3. NULL input

    json "{'val': null}" | eval result = urlencode(val)
    | # result: null
    

Compatibility

The urlencode() function is available since before Sonar 4.0.