I have a flow where I need to post data to a webservice. The variable data needs to be URLencoded in case there are characters that would otherwise break the URL. For example: "Hello World" would need to be converted to "Hello+World" or "Hello%20World" where the space character is converted to "+" or to its hex equivalent.
Is there a way to do urlEncoding of data in Switch short of writing a urlEncode function? Has anyone already written a urlEncode function that they could share?
Thanks.
URL Encode
URL Encode
Something like this kind of works, but when there are escaped characters in the string, I get weird results:
var theValue = "Hello World";
var args = new Array();
args[0] = "/usr/bin/python";
args[1] = "-c";
args[2] = "import urllib; print urllib.quote_plus('" + theValue + "')";
var myProc = new Process(args);
myProc.start();
myProc.waitForFinished(60);
job.log(-1, myProc.readStdout() );
so "Hello World" returns: "Hello+World"...
But, "Hello "World" returns: Hello+/var/folders/zb/5rzj0zb93n51lwx521q19tsc0000gp/T/FixtureTest38/inFolder22World
Not really sure what's going on there.
I'm going to try to write a standalone python script and then see if passing theValue as an argument might work.
Any other suggestions?
var theValue = "Hello World";
var args = new Array();
args[0] = "/usr/bin/python";
args[1] = "-c";
args[2] = "import urllib; print urllib.quote_plus('" + theValue + "')";
var myProc = new Process(args);
myProc.start();
myProc.waitForFinished(60);
job.log(-1, myProc.readStdout() );
so "Hello World" returns: "Hello+World"...
But, "Hello "World" returns: Hello+/var/folders/zb/5rzj0zb93n51lwx521q19tsc0000gp/T/FixtureTest38/inFolder22World
Not really sure what's going on there.
I'm going to try to write a standalone python script and then see if passing theValue as an argument might work.
Any other suggestions?
URL Encode
I got this to work by using a separate shell script and passing theValue as an argument. Then, escaping of the argument value seemed to work correctly. I'd love it if someone had a solution that didn't involve the need for the separate shell script file so this could be all encapsulated inside of Switch, but I think some characters like a quote or apostrophe when stuck into my one-liner python call is messing it up because those characters aren't escaped properly.
The weirdness that I was experiencing has something to do with how Switch is doing logging. The URL encoded result of "Hello"World" should be: "Hello+%22World" but Switch must do a substitution when it sees "%2" in the logging, and replaces it with:
"/var/folders/zb/5rzj0zb93n51lwx521q19tsc0000gp/T/FixtureTest38/inFolder2"
so, when I was expecting to see:
"Hello+%22World", I was seeing:
"Hello+/var/folders/zb/5rzj0zb93n51lwx521q19tsc0000gp/T/FixtureTest38/inFolder22World"
Here's where I netted out for my Switch Script:
var theValue = "hello "world";
var args = new Array();
args[0] = "/usr/bin/python";
args[1] = "/path/to/shellscript/urllib_escape.py";
args[2] = theValue;
var myProc = new Process(args);
myProc.start();
myProc.waitForFinished(60);
return myProc.readLineStdout()
And my shell script is just a couple of lines:
#!/usrbin/python
import urllib, sys
#print urllib.quote_plus(sys.argv[1])
sys.stdout.write( urllib.quote_plus(sys.argv[1]) ) # print sends a linefeed at the end, so we'll use sys.stdout.write instead
The weirdness that I was experiencing has something to do with how Switch is doing logging. The URL encoded result of "Hello"World" should be: "Hello+%22World" but Switch must do a substitution when it sees "%2" in the logging, and replaces it with:
"/var/folders/zb/5rzj0zb93n51lwx521q19tsc0000gp/T/FixtureTest38/inFolder2"
so, when I was expecting to see:
"Hello+%22World", I was seeing:
"Hello+/var/folders/zb/5rzj0zb93n51lwx521q19tsc0000gp/T/FixtureTest38/inFolder22World"
Here's where I netted out for my Switch Script:
var theValue = "hello "world";
var args = new Array();
args[0] = "/usr/bin/python";
args[1] = "/path/to/shellscript/urllib_escape.py";
args[2] = theValue;
var myProc = new Process(args);
myProc.start();
myProc.waitForFinished(60);
return myProc.readLineStdout()
And my shell script is just a couple of lines:
#!/usrbin/python
import urllib, sys
#print urllib.quote_plus(sys.argv[1])
sys.stdout.write( urllib.quote_plus(sys.argv[1]) ) # print sends a linefeed at the end, so we'll use sys.stdout.write instead
-
- TOP CONTRIBUTOR
- Posts: 628
- Joined: Mon Nov 29, 2010 8:45 pm
- Location: Alpharetta GA USA
- Contact:
URL Encode
function urlencode( url : String )
{
function toHex( c : Number )
{
var v = "", upNib = (c >> 4);
if (upNib > 9)
v += String.fromCharCode(55+upNib);
else
v += String.fromCharCode(48+upNib);
var lowNib = (c & 0x0f);
if (lowNib > 9)
v += String.fromCharCode(55+lowNib);
else
v += String.fromCharCode(48+lowNib);
return v;
}
var encodedStr = "";
for (var i=0; i<url.length; i++) {
var c = url.charAt(i);
switch (c) {
case ":":
case "/":
case "?":
case "#":
case "[:
case ]":
case "@":
case """:
case "'":
case " ":
encodedStr += '%' + toHex(c.charCodeAt(0));
break;
default:
encodedStr += c;
break;
}
}
return encodedStr;
}
var theValue = "hello "world";
return urlencode(theValue);
{
function toHex( c : Number )
{
var v = "", upNib = (c >> 4);
if (upNib > 9)
v += String.fromCharCode(55+upNib);
else
v += String.fromCharCode(48+upNib);
var lowNib = (c & 0x0f);
if (lowNib > 9)
v += String.fromCharCode(55+lowNib);
else
v += String.fromCharCode(48+lowNib);
return v;
}
var encodedStr = "";
for (var i=0; i<url.length; i++) {
var c = url.charAt(i);
switch (c) {
case ":":
case "/":
case "?":
case "#":
case "[:
case ]":
case "@":
case """:
case "'":
case " ":
encodedStr += '%' + toHex(c.charCodeAt(0));
break;
default:
encodedStr += c;
break;
}
}
return encodedStr;
}
var theValue = "hello "world";
return urlencode(theValue);
URL Encode
Thanks Dwight! That's exactly what I was looking for.
I made a slight modification to use a regular expression vs. switch/case, I'm posting it here for grins. Users can modify the switch/cases or the regular expression to add/delete characters to be encoded.
Also, I found out from Enfocus Support that the characters "%2", "%3", through "%10" are reserved in their logging function. That explains the weird substitution I was getting when logging my result. The "%2" in my string "Hello%20World" was being substituted with Switch's "folder path' value in their job.log() function.
Thanks again!
Jim
function urlEncodeValue( url : String )
{
function toHex( c : Number )
{
var v = "", upNib = (c >> 4);
if (upNib > 9)
v += String.fromCharCode(55+upNib);
else
v += String.fromCharCode(48+upNib);
var lowNib = (c & 0x0f);
if (lowNib > 9)
v += String.fromCharCode(55+lowNib);
else
v += String.fromCharCode(48+lowNib);
return v;
}
// Anything that matches this RegExp should be encoded.
// not a wordCharacter - . ~
var re = /[^w-.~]/;
var encodedStr = "";
for (var i=0; i<url.length; i++) {
var c = url.charAt(i);
if ( c.match(re) ) {
encodedStr += '%' + toHex(c.charCodeAt(0));
}
else {
encodedStr += c;
}
}
return encodedStr;
}
I made a slight modification to use a regular expression vs. switch/case, I'm posting it here for grins. Users can modify the switch/cases or the regular expression to add/delete characters to be encoded.
Also, I found out from Enfocus Support that the characters "%2", "%3", through "%10" are reserved in their logging function. That explains the weird substitution I was getting when logging my result. The "%2" in my string "Hello%20World" was being substituted with Switch's "folder path' value in their job.log() function.
Thanks again!
Jim
function urlEncodeValue( url : String )
{
function toHex( c : Number )
{
var v = "", upNib = (c >> 4);
if (upNib > 9)
v += String.fromCharCode(55+upNib);
else
v += String.fromCharCode(48+upNib);
var lowNib = (c & 0x0f);
if (lowNib > 9)
v += String.fromCharCode(55+lowNib);
else
v += String.fromCharCode(48+lowNib);
return v;
}
// Anything that matches this RegExp should be encoded.
// not a wordCharacter - . ~
var re = /[^w-.~]/;
var encodedStr = "";
for (var i=0; i<url.length; i++) {
var c = url.charAt(i);
if ( c.match(re) ) {
encodedStr += '%' + toHex(c.charCodeAt(0));
}
else {
encodedStr += c;
}
}
return encodedStr;
}