PDA

View Full Version : URLEncode a string in InstallScript



ThisIsEd
10-11-2010, 05:27 PM
I'm passing some user-supplied values to a REST endpoint in InstallScript and need to encode the values so they are passed correctly. Here's a stripped down example:

User provides "J&R Brothers" and I need to call:
http://www.someurl.com/page.php?name=J%26R%20Brothers

It appears there is no built in functionality in InstallScript to url encode a string. Anyone know of a pre-written function? Or perhaps a Windows API call that I could use to encode this url? Seems like a problem that shouldn't require reinventing the wheel.

Would love to hear how others handle this. Thanks!

MichaelU
10-12-2010, 11:47 AM
A lot depends on how you're calling this. If you're just using XCopyFile to perform a GET, you'll probably hit the problem as you describe it. If instead you're driving something like Internet Explorer via COM, the object you're will likely offer this capability implicitly or explicitly in its API.

ThisIsEd
10-12-2010, 02:00 PM
Hi Michael,

The more complicated explanation is that we're not really making a network request but actually calling our own software, making a POST to an REST endpoint via the commandline. i.e.

program.exe --post /path/to/end/point "variable=value&variable2=value2"

We don't have an urlencoding built into our code currently and I was hoping I could find something to use during the install.

Currently I have a janky function that is just a bunch of StrReplace calls. If I have to roll my own, I'd prefer to actually do it based on the character codes, but can't figure out how to get that value in InstallScript.

MichaelU
10-13-2010, 09:54 AM
Something along the lines of nChar = szRaw[nIndex] should be all you need to get the character codes (NUMBER and STRING variables).

ThisIsEd
10-13-2010, 06:21 PM
Awesome, thanks! That gets me the code in decimal, anything built into InstallScript that I'm missing that'll do the decimal to hex conversion?

ThisIsEd
10-13-2010, 06:41 PM
Nevermind, Sprintf(svHex, "%lX", nChar) converts a decimal to hexadecimal just fine!

ThisIsEd
10-13-2010, 07:36 PM
In the spirit of sharing, here's the url encoding function I threw together in InstallScript.


function STRING URLEncode(szString)
STRING szEncodedString, svHex;
NUMBER n, nLength, nChar;
begin
n = 0;
szEncodedString = "";
nLength = StrLength(szString);
while (n <= nLength)
nChar = szString[n];
//Check decimal value to see if we need to encode
if ((nChar > 0) && (nChar <= 47))
|| ((nChar >= 58) && (nChar <=64))
|| ((nChar >= 91) && (nChar <= 96))
|| ((nChar >= 123) && (nChar <= 255)) then

Sprintf(svHex, "%lX", nChar);
svHex = "%"+svHex;
else
Sprintf(svHex, "%c", nChar); //don't encode it
endif;
szEncodedString = szEncodedString+svHex;
n++;
endwhile;
return szEncodedString;
end;

MichaelU
10-14-2010, 11:38 AM
Overall that sounds good. One minor thing: you may want to use %02lX instead of %lX in order to properly handle values below 0x10.