Page 1 of 1

A function for url encoding

Posted: Sun Jan 08, 2017 1:01 am
by LaZyLion
Hi all,

I got to playing around with functions and came up with a way to url encode text.
I regularly use /tool fetch to send information to a php script on my web server.
I run my own homemade DYDNS variant and it helps to have the information properly encoded so it arrives intact.


The code:

Create a file called urlencode.fnc
:if ([:len $this ]>0) do={
:local chars " \"%&"; 
:local subs { "%20"; "'"; "%25"; "%26" }
:local that "";
:for i from=0 to=([:len $this]-1) do={ 
	:local s [:pick $this $i];
	:local x [:find $chars $s]
	:if ([:len $x]>0) do={
		:set $s ($subs->$x)	
	}
	:set $that ($that . $s)
}
:return $that;
}


Create another file called test.rsc
# test function

# create fucntion from file
:local urlencode [:parse [/file get [/file find name="url_encoder.fnc"] contents] ];


# string to be tested
:local names "our names are 'bob' & 'doug'";


# usage
:local op [$urlencode this=$names];
:put $op;

Upload both files to your router.


From the CLI type: /import test.rsc

... and voila!



Notes:
You'll notice that this version only encodes four things:
  • space
    double quote
    percent sign
    ampersand
The reason for this is those are the ones I use. You can can add your own as needed:
characters to be subbed go on line 2, and the substitutions on line three.
Make sure you maintain the correct order, formatting and syntax.

There are over 200 characters which can be encoded which would make the script rather long,
so stick to using the ones you expect to need.

See the encoding table here: http://www.w3schools.com/tags/ref_urlencode.asp
Use php urldecode() to restore (or equivalent in whatever language you are using).

Don't let the backslash in line 2 fool you. Double quotes inside double quotes have to 'escaped'.

The function uses the variable $this to get it's parameter, ie: text to be encoded.

Info on functions in RouterOS scripting can be found here: http://wiki.mikrotik.com/wiki/Manual:Sc ... #Functions

Hope someone finds this useful. Happy coding.

Tested on 6.37.3
Jan 2017

Re: A function for url encoding

Posted: Mon Feb 20, 2017 9:47 pm
by AffinityNetworks
Fantastic, thank you for sharing. :D

Thats a keeper!

Re: A function for url encoding

Posted: Fri Apr 26, 2019 2:39 am
by eminkaplan
Thanks, its work on ros 6.44.2

Re: A function for url encoding

Posted: Wed Jul 24, 2024 7:50 pm
by Radek01
Is there a way to do this conversion including national characters with diacritics (German, Czech and other languages)? I tried to make a similar script, but I failed just on the diacritics. RouterOS probably takes "e", "é" and "ě" as the same characters and I don't know if this is solvable.

Re: A function for url encoding

Posted: Fri Jul 26, 2024 6:00 pm
by rextended
Some user write a tons of script for character encoding/decoding and mikrotik add some commands on v7....

Re: A function for url encoding

Posted: Wed Jul 31, 2024 2:22 am
by Apachez
Some user write a tons of script for character encoding/decoding and mikrotik add some commands on v7....
v6 seems still to be the LTS edition as of writing so no wonder why people needs to still reinvent the wheel when using RouterOS?