Runtime verified Differs from official docs Test scripts included

Syntax

URLEncode(urlToEncode[, encodeAllChars, encodeAllStrings])  →  string
1–3 arguments

Parameters

Name Type Required Description
urlToEncode string | number Yes Value to make safe for use in a URL
encodeAllChars string | boolean | number No Switches on full encoding of the query string; off by default. Accepts true, false, 1, 0, "true", "false", "1", "0"
encodeAllStrings string | boolean | number No Switches on encoding of the whole input, not just a query string; off by default. Accepts true, false, 1, 0, "true", "false", "1", "0"

One to three arguments. Each flag gave the same result whether written as 1/0, true/false or as one of those words in quotes.

Example

%%=URLEncode("https://example.org/go?promo=spring sale&tags=a,b")=%%

Renders https://example.org/go?promo=spring%20sale&tags=a,b.

A value that is not a URL needs encodeAllStrings, otherwise it comes back unchanged:

%%=URLEncode("spring sale", 0, 1)=%%

Renders spring%20sale.

Return value

string — the encoded form of the input.

An empty input gives an empty string. There is no special value to test for.

Behaviour

Argument count. An email that calls URLEncode() or passes four arguments cannot be saved: the content API answers HTTP 400 with “The AMPscript syntax isn’t valid”.

By default only spaces after the question mark change. https://example.org/go?promo=spring sale&tags=a,b renders as https://example.org/go?promo=spring%20sale&tags=a,b. A value without a question mark comes back untouched: spring sale stays spring sale, and a b&c stays a b&c. Passing both flags as off (0, 0 or false, false), or only the first flag as off, gives the same result.

encodeAllChars also encodes the comma. With the first flag on and the second off or left out, the URL renders as https://example.org/go?promo=spring%20sale&tags=a%2Cb. The = and & stay as they are, spaces stay %20, and the escape is written in upper case.

encodeAllStrings makes any input eligible. URLEncode("spring sale", 0, 1) renders spring%20sale. With both flags on the result is the same, spring%20sale: spaces are never written as +.

Flag spellings are interchangeable. 1, true, "1" and "true" all act as on; 0, false, "0" and "false" all act as off.

A flag value of 2 acts as on. URLEncode("spring sale", 2, 2) renders spring%20sale.

Numbers and empty strings are accepted. URLEncode(4821) gives 4821, and URLEncode("") gives an empty string.

Show Next test script
<!-- Marketing Cloud Next test script for URLEncode. Paste it into the HTML component of a
     Next email and open the preview. Each line prints the result Next gave when the call
     was rendered on its own (expect) next to the result of this run (got).
     Assembled from single-call test emails (dates on the page); it has not been
     run as one email. Calls that make Next fail are listed separately. -->
<p>c1 expect=[a b&amp;c] got=[%%=URLEncode("a b&c")=%%]</p>
<p>c2 expect=[https://example.org/go?promo=spring%20sale&amp;tags=a,b] got=[%%=URLEncode("https://example.org/go?promo=spring sale&tags=a,b",0,0)=%%]</p>
<p>c3 expect=[spring%20sale] got=[%%=URLEncode("spring sale",0,1)=%%]</p>
<p>c4 expect=[spring%20sale] got=[%%=URLEncode("spring sale",1,1)=%%]</p>
<p>c5 expect=[https://example.org/go?promo=spring%20sale&amp;tags=a%2Cb] got=[%%=URLEncode("https://example.org/go?promo=spring sale&tags=a,b",1,0)=%%]</p>
<p>c6 expect=[spring sale] got=[%%=URLEncode("spring sale")=%%]</p>
<p>c7 expect=[https://example.org/go?promo=spring%20sale&amp;tags=a,b] got=[%%=URLEncode("https://example.org/go?promo=spring sale&tags=a,b")=%%]</p>
<p>c8 expect=[https://example.org/go?promo=spring%20sale&amp;tags=a,b] got=[%%=URLEncode("https://example.org/go?promo=spring sale&tags=a,b",false,false)=%%]</p>
<p>c9 expect=[https://example.org/go?promo=spring%20sale&amp;tags=a%2Cb] got=[%%=URLEncode("https://example.org/go?promo=spring sale&tags=a,b",true,false)=%%]</p>
<p>c10 expect=[spring%20sale] got=[%%=URLEncode("spring sale",false,true)=%%]</p>
<p>c11 expect=[spring%20sale] got=[%%=URLEncode("spring sale",true,true)=%%]</p>
<p>c12 expect=[https://example.org/go?promo=spring%20sale&amp;tags=a,b] got=[%%=URLEncode("https://example.org/go?promo=spring sale&tags=a,b",0)=%%]</p>
<p>c13 expect=[https://example.org/go?promo=spring%20sale&amp;tags=a%2Cb] got=[%%=URLEncode("https://example.org/go?promo=spring sale&tags=a,b",1)=%%]</p>
<p>c14 expect=[https://example.org/go?promo=spring%20sale&amp;tags=a,b] got=[%%=URLEncode("https://example.org/go?promo=spring sale&tags=a,b",false)=%%]</p>
<p>c15 expect=[https://example.org/go?promo=spring%20sale&amp;tags=a%2Cb] got=[%%=URLEncode("https://example.org/go?promo=spring sale&tags=a,b",true)=%%]</p>
<p>c16 expect=[spring%20sale] got=[%%=URLEncode("spring sale","1","1")=%%]</p>
<p>c17 expect=[spring%20sale] got=[%%=URLEncode("spring sale","true","true")=%%]</p>
<p>c18 expect=[spring%20sale] got=[%%=URLEncode("spring sale","0","1")=%%]</p>
<p>c19 expect=[spring%20sale] got=[%%=URLEncode("spring sale","false","true")=%%]</p>
<p>c20 expect=[https://example.org/go?promo=spring%20sale&amp;tags=a%2Cb] got=[%%=URLEncode("https://example.org/go?promo=spring sale&tags=a,b","1","0")=%%]</p>
<p>c21 expect=[https://example.org/go?promo=spring%20sale&amp;tags=a%2Cb] got=[%%=URLEncode("https://example.org/go?promo=spring sale&tags=a,b","true","false")=%%]</p>
<p>c22 expect=[spring%20sale] got=[%%=URLEncode("spring sale",2,2)=%%]</p>
<p>c23 expect=[4821] got=[%%=URLEncode(4821)=%%]</p>
<p>c24 expect=[] got=[%%=URLEncode("")=%%]</p>

Show calls that fail in Next (one email each)
<!-- Run each call below in its own Next email: one of them in a combined email
     would stop the whole preview or block the save. -->

<!-- email 1: expect that the save is rejected (HTTP 400) -->
%%=URLEncode()=%%

<!-- email 2: expect that the save is rejected (HTTP 400) -->
%%=URLEncode("https://example.org/go?promo=spring sale&tags=a,b",0,0,0)=%%

Differs from official docs

  • Available in Next. The official reference marks URLEncode as not available in Marketing Cloud Next. It saved and rendered correctly in a Next email (see the examples above). Details
  • encodeAllChars output. The official examples show = escaped as %3d and spaces written as + once the flag is on. In Next, = and & are kept, spaces stay %20, and only the comma is escaped, as %2C; with both flags on, spring sale becomes spring%20sale. Details

Availability & differences

PlatformAvailable
Marketing Cloud EngagementYes
Marketing Cloud NextYes, from API 68.0

See also