I am trying to do a “/tool fetch url=”$urlstring" dst-path=“/$file” where $urlstring is a string with bunch of data including the different values gathered from “/system routerboard print”. This in turn is a poor mans way of making a http post to a server to register data about every router.
Problem is that many of the values contains spaces, hyphens and other characters that does not work well in url’s and they need to be escaped (url encoded). So, have anyone done that before? What I basically need is a way to replace different characters in a string, ie - with %2D, space with %20 etc.
You can loop through the string, checking each character and substituting as needed. Add additional :if statements for any other desired characters. The starting and ending brackets are for copying / pasting into the terminal (because of :local variables), and they can be removed in your final script.
Very good, skot!
I’m writing a script to receive SMS and send inbox text via http via “fetch” command.
I just need something to handle spaces in sms text…
I solved the issue, when I was working at this.
I remember that it wil be necessary to reformat sms text by inserting “?” instead of sms spaces.
It was working well, but unfortunately the RB750UP I was working on get destroyed by some GSM modem issues, as you can see on those posts: http://forum.mikrotik.com/t/rb750up-3g-usb-freezes-completely/54120/1 http://forum.mikrotik.com/t/rb750up-dead/53960/1
So I lost all my work (not saved yet) and I lost also any big interest in using mikrotik as SMS gateway, beacause Routeros is too much unreliable when used with GSM modems.
I noticed lot of issues also with microSD storage, smb service and also some issue with USB storage.
I tested lot of Routeros versions, on RB750UP and RB493G.
I tried to replace a url “0/24” from “192.168.1.0/24”
Unfortunately it doesnt work with your script.
I have tried to do a test with urlstring, it works. Only when comes to replacing IP it doesnt work. It only replace 1 character and not more than that?
Correct. That script above loops through the url one character at a time, replacing the ones you want. If you want to replace a substring, it will have to be done differently, something like this:
To use it u place the following at the beginning of your script:
/system script run "Functions"
:global urlEncode
And at the point you want to urlEncode something the following:
:global Result ""
:local runFunc [:parse (":global Result; :local input \"Place your text to urlEncode here!\"; " . $urlEncode . ":set Result \$output")]
$runFunc
You can then pickup the urlencoded string in $Result
Also - check this out, this is how we now use the functions . (please, see the “sticky” Functions and function parameters thread in this forum section!)
Then you put it in “Functions” script, and - when needed - you do the same in the beginning of your script:
/system script run "Functions"
:global urlEncode
But then, using it is much simpler, for example:
:local original "This is a (simple) string";
:local result [$urlEncode $original];
:put "The original text is:";
:put $original;
:put "And the escaped result is:";
:put $result;
Or even:
:put "This is a quickly escaped string: $[$urlEncode "Escape this: text"] - simple, isn't it?";