# `urlEncode`

## Description

Returns the URL-encoded version of a string.

URL encoding replaces special characters with escape sequences (for example, spaces become `%20`) so values can be safely transmitted in URLs. Use `urlEncode` when preparing data for output, such as with log forwarders.

## Syntax

Like many functions in DataPrime, `urlEncode` supports [two notations](https://coralogix.com/docs/dataprime/language-reference/functions-reference/index.md), **function** and **method** notation. These interchangeable forms allow flexibility in how you structure expressions.

```dataprime
urlEncode(string: string): string
```

```dataprime
(string: string).urlEncode(): string
```

## Arguments

| Name   | Type   | Required | Description              |
| ------ | ------ | -------- | ------------------------ |
| string | string | **true** | The string to URL encode |

## Example

**Use case: Encode query string parameters**

Suppose you want to parse query string parameters and encode any values that contain spaces. Consider this document:

```json
{
  "domain": "https://www.coralogix.com",
  "path": "/home",
  "query_string": "name=Tom Kosman&b=c&c=d"
}
```

First, extract the parameters into an object using `kv`:

```text
extract query_string into query_string_parameters using kv(pair_delimiter='&', key_delimiter='=')
```

This produces:

```json
{
  "domain": "https://www.coralogix.com",
  "path": "/home",
  "query_string": "name=Tom Kosman&b=c&c=d",
  "query_string_parameters": {
    "name": "Tom Kosman",
    "b": "c",
    "c": "d"
  }
}
```

Now encode the `name` field:

### Example query

```dataprime
replace query_string_parameters.name with urlEncode(query_string_parameters.name)
```

```dataprime
replace query_string_parameters.name with query_string_parameters.name.urlEncode()
```

### Example output

```json
{
  "domain": "https://www.coralogix.com",
  "path": "/home",
  "query_string": "name=Tom Kosman&b=c&c=d",
  "query_string_parameters": {
    "name": "Tom%20Kosman",
    "b": "c",
    "c": "d"
  }
}
```
