URL encodes the contents of a string field. The urlEncode() function converts special characters like parentheses, pipes, and spaces into their URL-safe percent-encoded equivalents. By default, the encoded result is returned in a new field named _urlencode.

ParameterTypeRequiredDefault ValueDescription
asstringoptional[a] _urlencode Name of output field.
field[b]stringrequired   Name of input field that contains the value for URL encoding.
typestringoptional[a]   Type of encoding.

[a] Optional parameters use their default value unless explicitly set.

[b] The parameter name field can be omitted.

Hide omitted argument names for this function

Show omitted argument names for this function

urlEncode() Syntax Examples

This example shows how to URL encode a LogScale query string so it can be safely included in a URL parameter.

logscale
urlEncode(bar)

If input was bar := "tail(1) | count()", it would return:

"_urlencode"
"tail%281%29+%7C+count%28%29"

This example shows how to URL encode an email address so it can be safely included in a URL parameter.

logscale
urlEncode(bar)

If input was bar := "example@example.com", it would return:

"_urlencode"
"example%40example.com"

This example shows how to URL encode a field containing a string so it can be safely included in a URL parameter.

logscale
urlEncode(bar)

If input was bar := "Title with spaces/special characters!", it would return:

"_urlencode"
"Title+with+spaces%2Fspecial+characters%21"

urlEncode() Examples

Click + next to an example below to get the full details.

Encode Search Query For URL Usage

URL encode a search query string using the urlEncode() function

Query
logscale
search_query = *
| urlEncode(field=search_query, as=encoded_search_query)
Introduction

In this example, the urlEncode() function is used to encode a search query string that may contain special characters, making it safe for use in URLs.

Example incoming data might look like this:

@timestampsearch_query
2025-01-15T10:00:00Zstatus=error&severity>3
2025-01-15T10:01:00Zhost=web-server1 #production
2025-01-15T10:02:00Zsource=*/apache/access.log
2025-01-15T10:03:00Zmethod=GET&path=/api/v1/users
2025-01-15T10:04:00Zerror message = Connection refused
Step-by-Step
  1. Starting with the source repository events.

  2. logscale
    search_query = *

    Filters for all events that contain the field search_query. This ensures that only events with search queries are processed.

  3. logscale
    | urlEncode(field=search_query, as=encoded_search_query)

    URL encodes the content of the search_query field and returns the encoded result in a new field named encoded_search_query. The function replaces special characters with their percent-encoded equivalents, making the string safe for use in URLs.

  4. Event Result set.

Summary and Results

The query is used to convert search query strings containing special characters into URL-safe format. URL encoding replaces special characters with a percent sign (%) followed by a two-digit hexadecimal code that represents the character.

URL encoding is essential when constructing URLs with dynamic parameters, ensuring that user input does not break URL structure or cause parsing issues in web applications, APIs, and search systems.

This query is useful, for example, to prepare search queries for use in API calls, URL parameters, or when generating links to LogScale dashboards with specific search criteria.

Sample output from the incoming example data:

search_queryencoded_search_query
status=error&severity>3status%3Derror%26severity%3E3
host=web-server1 #productionhost%3Dweb-server1%20%23production
source=*/apache/access.logsource%3D%2A%2Fapache%2Faccess.log
method=GET&path=/api/v1/usersmethod%3DGET%26path%3D%2Fapi%2Fv1%2Fusers
error message = Connection refusederror%20message%20%3D%20Connection%20refused

Note that special characters such as spaces, ampersands (&), equals signs (=), forward slashes (/), and hash symbols (#) are converted to their percent-encoded equivalents.