Page 1 of 1
Usage of encodeURI
Posted: Fri Nov 17, 2017 6:14 pm
by patgilmour
Hi,
Does anyone have a simple working example of how to use the HTTP class's encodeURI(theURL) method?
For example, this doesn't work:
Code: Select all
// Build param
var theParam = "this is a test";
// Encode Param
var theParamEncoded = encodeURI(theParam);
// Build URL
var theURL = "http://www.example.com/httplogerror.php?message=" + theParamEncoded;
//etc.
And if I try
Code: Select all
var theParamEncoded = HTTP.encodeURI(theParam)
or replacing HTTP with a new http object, the method replaces the spaces in my string with paths!!!
By the way, encodeURIComponent() returns the same.
Any help much appreciated.
Pat
Re: Usage of encodeURI
Posted: Fri Nov 17, 2017 6:24 pm
by patgilmour
Could this be because Enfocus Switch Scripter has reserved '%20' for its own special usage?
Re: Usage of encodeURI
Posted: Mon Nov 20, 2017 9:51 am
by Padawan
I did a test where I write theParamEncoded to a file and then the file contains "this%20is%20a%20test". So I think you're right, it is because of the special meaning of %2 in log messages.
If there would be other people not knowing what we are talking about, I found the explanation in documentation (Environment class):
Log: Multiple variable parts
The message argument can use the special placeholders "%11", "%12" through "%19" to access the corresponding segment (indexed 1 through 9) of the extra argument interpreted as a list of comma-separated values. If the specified segment does not exist the empty string is used. There is no support for quoted strings so individual values in the list can not contain commas.
Code: Select all
job.log(1, "sample: %11 and %12", "one,two"); //"sample: one and two"
Note: Placeholders %2 to %10 are reserved for Switch. Do not use them in scripts.
Re: Usage of encodeURI
Posted: Mon Nov 20, 2017 8:20 pm
by patgilmour
@Padawan - yes, it must be that. Of course, it's not me using %20 in the script, it is Enfocus Switch
I guess I'll have to set up an http request and see if the URI is, in fact, correctly encoded.
Thanks for posting the info.
===UPDATE===
Just to confirm, the
%2 is reserved by Switch so if you encode your URLs and the result is something like:
Code: Select all
http://www.example.com?string=my%20string
You will see strange results in the Messages Log.
In spite of this, the URL you create will be correctly encoded, so this does work:
Code: Select all
// Set URL
var theURL = "http://www.example.com?string=my string";
// Encode the URL
var encodedURI = HTTP.encodeURI(theURL);
This is obviously a ridiculous shortfall in a modern scripting environment where http requests are so common. Forewarned is forearmed!