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
STRisnull, the function returnsnull. - If
STRis not a string type, the function returnsnull. - If an encoding error occurs, the function returns
null. - If an invalid character set name is specified for
CHARSET, error code90850is raised.
Error codes
| Error code | Description |
|---|---|
| 90850 | An 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.
-
URL-encode a URI
table limit=5 WEB_APACHE_SAMPLE | eval result = urlencode(uri) | fields uri, result -
URL-encode a Korean string
json "{'val': '로그분석'}" | eval result = urlencode(val) | # result: "%EB%A1%9C%EA%B7%B8%EB%B6%84%EC%84%9D" -
NULL input
json "{'val': null}" | eval result = urlencode(val) | # result: null
Compatibility
The urlencode() function is available since before Sonar 4.0.