URL encoding in Velocity scripts? | Community
Skip to main content
Bishoy_Riad
Level 2
Nov 29, 2019 5:35 PM UTC
Solved

URL encoding in Velocity scripts?

Hi Marketo Nation,

Curious if there's a standard for URL encoding in Velocity scripts. Below is a {{my.Calendly_Link}} custom token script that outputs a Calendly link. However, the URL printed at the end has non-ASCII characters such as = (equals sign), as well as & (ampersand).

Here's the current script (without URL encoding) at line 12:

#if(($lead.Owner_Calendly_Link__c.isEmpty() || $lead.Owner_Calendly_Link__c == "-") && $lead.Number_of_Opportunities == "0")
#set($calendlylink = "calendly.com/fresh-tracks-canada")
#elseif($OpportunityList.get(0).Owner_Calendly_Link__c.isEmpty() || $OpportunityList.get(0).Owner_Calendly_Link__c == "-")
#set($calendlylink = "calendly.com/fresh-tracks-canada")
#elseif((!$lead.Owner_Calendly_Link__c.isEmpty() && $lead.Owner_Calendly_Link__c != "-") && $lead.Number_of_Opportunities == "0")
#set($calendlylink = "${lead.Owner_Calendly_Link__c}")
#elseif(!$OpportunityList.get(0).Owner_Calendly_Link__c.isEmpty() && $OpportunityList.get(0).Owner_Calendly_Link__c != "-")
#set($calendlylink = "${OpportunityList.get(0).Owner_Calendly_Link__c}")
#else
#set($calendlylink = "calendly.com/fresh-tracks-canada")
#end
https://${calendlylink}/1?text_color=000000&primary_color=e42f3a‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Based on my front-end testing, it doesn't seem necessary - just practicing some due diligence.

Thanks!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
SanfordWhiteman
SanfordWhitemanAccepted solution
Level 10
Nov 29, 2019 7:20 PM UTC

"&" and "=" are very much ASCII characters, ASCII #38 and #61 respectively.

You appear to be talking about HTML encoding, not URL encoding. These are very different things. You don't want to URL-encode the "=", that will break your query string. (You don't need to HTML-encode "=" either, but it would be harmless.)

The "&" character should indeed be HTML encoded to not break if you use special sequences in query params (and it certainly is possible to break a link, you just have to test with the right/wrong chars):

https://${calendlylink}/1?${esc.html("text_color=000000&primary_color=e42f3a")}‍

5 replies

SanfordWhiteman
SanfordWhitemanAccepted solution
Level 10
Nov 29, 2019 7:20 PM UTC

"&" and "=" are very much ASCII characters, ASCII #38 and #61 respectively.

You appear to be talking about HTML encoding, not URL encoding. These are very different things. You don't want to URL-encode the "=", that will break your query string. (You don't need to HTML-encode "=" either, but it would be harmless.)

The "&" character should indeed be HTML encoded to not break if you use special sequences in query params (and it certainly is possible to break a link, you just have to test with the right/wrong chars):

https://${calendlylink}/1?${esc.html("text_color=000000&primary_color=e42f3a")}‍
Translate
Bishoy_Riad
Level 2
Nov 29, 2019 7:26 PM UTC

You're right, those are ASCII characters, I think my doubts were around them being "unsafe". Which they are, and this is exactly the resolution I was looking for. Thanks!

Translate
SanfordWhiteman
Level 10
Nov 29, 2019 7:32 PM UTC

"=" is not unsafe in a URL. If you're using it to delimit query names from query values, it must not be escaped.

Translate