Usage of encodeURI

Post Reply
patgilmour
Newbie
Posts: 13
Joined: Mon Jul 06, 2015 10:56 pm

Usage of encodeURI

Post 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
patgilmour
Newbie
Posts: 13
Joined: Mon Jul 06, 2015 10:56 pm

Re: Usage of encodeURI

Post by patgilmour »

Could this be because Enfocus Switch Scripter has reserved '%20' for its own special usage?
Padawan
Advanced member
Posts: 358
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: Usage of encodeURI

Post 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.
patgilmour
Newbie
Posts: 13
Joined: Mon Jul 06, 2015 10:56 pm

Re: Usage of encodeURI

Post 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!
Post Reply