UrlEncode

Performs percent-encoding on a URL.

These characters have reserved uses in URLs: ! # $ & ' ( ) * + , / : ; = ? @ [ ]. Reserved characters serve a specific purpose in the context of a URL, so they must be encoded when they’re included in a URL for another purpose. For example, the question mark (?) character is used to separate the base URL from a query string. If the text that’s specified within the query string includes a question mark character, that character must be encoded.

note: This function performs character encoding. It doesn’t encrypt the query parameters in the URL. To encrypt values in a query string for use in CloudPages, use the AMPscript CloudPagesURL() function.

Syntax

UrlEncode(1, 2)

Properties

Ordinal Type Description
1 String Required. The URL to encode. The specified value must be a complete URL in order to be percent-encoded.
2 Boolean Specifies the extent of the encoding. If the value is true, all reserved characters in the URL are converted to percent-encoded values, except for space characters, which are encoded with the plus sign (+). If the value is false, space characters are converted to %20, but no other reserved characters are encoded. The default value is false.

Example

This example shows how URLs are percent-encoded when the value of the second operand is true or false.

<script runat="server">
  var baseURL = "http://www.example.com?value=12+3 12;3";
  var urlEncodeDefault = Platform.Function.UrlEncode(baseURL);
  var urlEncodeOperand2True = Platform.Function.UrlEncode(baseURL, true);
</script>

<table>
  <tr>
    <th>Description</th>
    <th>Value</th>
  </tr>
  <tr>
    <td>Base URL</td>
    <td><ctrl:var name="baseURL" /></td>
  </tr>
  <tr>
    <td>Value when operand 2 equals false (default)</td>
    <td><ctrl:var name="urlEncodeDefault" /></td>
  </tr>
  <tr>
    <td>Value when operand 2 equals true</td>
    <td><ctrl:var name="urlEncodeOperand2True" /></td>
  </tr>
</table>

The example outputs these values.

Description Value
Base URL http://www.example.com?value=12+3 12;3
Value when operand 2 equals false (default) http://www.example.com?value=12+3%2012;3
Value when operand 2 equals true http://www.example.com?value%3d12%2b3+12%3b3