Question

The best way/method to encode an URL?

  • 14 April 2024
  • 9 replies
  • 57 views

Userlevel 4
Badge +4

Hello, 

 

I have created a complex link with around 25 parameters that I want to use in a button link in Mailersend.com.

The link  looks like: http://domain.com/?parameter1=value1&parameter2=value2&parameter3=value3 etc.

The problem is that some parameters contain special characters, spaces etc.. Therefore I need to encode the URL. 

Which is the best way to do this? I saw that Zapier has a “URL Encode” feature. Do I have to encode each parameter value (i.e. value1, value2, value 3 etc.) individually or can I encode the entire URL?

What is the best way? 

Thanks for your help, 
Michael


9 replies

Userlevel 7
Badge +11

Hi @michael291! 👋

It’s possible to encode the entire URL.

If you add the URL along with the parameters into the Input field on the Formatter (Text > URL Encode) action that should ensure it encodes the entire URL.

For example:

2d8563a8b136682f94e617967436cf13.png

Can you give that a try and let us know if that works as expected?

Userlevel 4
Badge +4

Hi @SamB 

I have tried that already and it does not work. If I encode the entire URL and paste it into a browser, the browser cannot decode the URL correctly.

 

The entire URL will be encoded as: 

https%3A//domain.com/page/%3Ffi%3DHanselmann%20AG%26vo%3DHans%26na%3DHanselmann%26an%3DHerr%26fu%3DMusterfunktion%26te%3D%2B41448889900%26em1%3Dhans.hanselmann%40test.com%26st1%3DHans%20Huber-Strasse%2045%26zz1%3DZweite%20Adresszeile%20123%26or1%3DSolothurn%26plz1%3D4500%26la1%3DSchweiz%26fi2%3DMeiermann%20AG%26zh%3DMartin%20Meiermann%26em2%3Dmartin.meiermann%40test.com%26st2%3DMeierhofpl.%26zz2%3DZweite%20Adresszeile%20456%26or2%3DZ%C3%BCrich%26plz2%3D8049%26la2%3DSchweiz%26uid%3DCHE-123.456.789%26zki%3D606282000Xdgrfgdfg609%26zai%3D6062fkgue3r01388606%26be%3D

I figured out that the domain will work if it will look like: 
https://domain.com/page/?fi=Hanselmann%20AG&vo=Hans&na=Hanselmann&an=Herr&fu=Musterfunktion&te=%2B41448889900&em1=hans.hanselmann%40test.com&st1=Hans%20Huber-Strasse%2045&zz1=Zweite%20Adresszeile%20123&or1=Solothurn&plz1=4500&la1=Schweiz&fi2=Meiermann%20AG&zh=Martin%20Meiermann&em2=martin.meiermann%40test.com&st2=Meierhofpl.&zz2=Zweite%20Adresszeile%20456&or2=Z%C3%BCrich&plz2=8049&la2=Schweiz&uid=CHE-123.456.789&zki=606282000Xdgrfgdfg609&zai=6062fkgue3r01388606&be=

So I think only the values should be encoded and the url should list the parameters like: 
/?parameter1=value1&parameter2=John%20Doe… etc.

What am I doing wrong? 

Thanks for your help, 
Michael

Userlevel 7
Badge +14

Hi @michael291 

Try this:

Formatter > Text > Replace

Replace all spaces with a +

Userlevel 4
Badge +4

Hi @Troy Tessalone

thanks for your reply. I tested it and it does not work. 

The parameter values can contain special characters, umlauts, spaces etc. So I guess I have to encode each parameter value. 

Userlevel 4
Badge +4

Any suggestions?

Userlevel 7
Badge +14

@michael291 

Did you try your suggestion?

The parameter values can contain special characters, umlauts, spaces etc. So I guess I have to encode each parameter value. 

 

1 Code step could be used to encode the parameter values and create the full URL.

https://www.w3schools.com/jsref/jsref_encodeuri.asp

https://www.w3schools.com/jsref/jsref_encodeuricomponent.asp

 

Feedback from ChatGPT

var uri = "https://example.com/path with spaces/";
var encodedURI = encodeURI(uri);
console.log(encodedURI); // Output: "https://example.com/path%20with%20spaces/"

 

var queryParam = "user name@example.com";
var encodedQueryParam = encodeURIComponent(queryParam);
console.log(encodedQueryParam); // Output: "user%20name%40example.com"

 

 

Userlevel 4
Badge +4

Hi @Troy Tessalone , 

great, thanks a lot for the tip! Just one question. How do I get the output? 

Userlevel 4
Badge +4

I figured it out (thanks to the AI feature in Zapier). 

 

I used Zapier code and used this. I know that the code can still be improved.

// Extract the 'parameter1' and 'parameter2' parameters from the inputData object
const parameter1 = inputData.parameter1;
const parameter2 = inputData.parameter2;

// Create a URL string with the 'fi1' and 'vo' parameters
// The 'fi1' and 'vo' parameters are encoded using the encodeURIComponent function
const url =
"https://domain.com/page-xyz/?parameter1=" +
encodeURIComponent(parameter1) +
"&parameter2=" +
encodeURIComponent(parameter2);

// Log the encoded URL to the console
console.log(url);

// Set the output variable to an object with the encoded URL
output = { url: url };

 

Userlevel 7
Badge +11

Thanks for the update here @michael291.

Really pleased to hear you were able to get it encoding the URL correctly using an AI-powered Code step - nice work! 🙌  And a big thank you to @Troy Tessalone for helping to you point you in the right direction! 🤗  ​

You mentioned that “the code can still be improved” - do you need any help with that or are you all set for now?

Do let us know if there’s anything else you need help with at all.

Reply