cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
ThisIsEd
Level 4

URLEncode a string in InstallScript

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!
Labels (1)
0 Kudos
(7) Replies
MichaelU
Level 12 Flexeran
Level 12 Flexeran

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.
0 Kudos
ThisIsEd
Level 4

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.
0 Kudos
MichaelU
Level 12 Flexeran
Level 12 Flexeran

Something along the lines of [FONT="Courier New"]nChar = szRaw[nIndex][/FONT] should be all you need to get the character codes (NUMBER and STRING variables).
0 Kudos
ThisIsEd
Level 4

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?
0 Kudos
ThisIsEd
Level 4

Nevermind, Sprintf(svHex, "%lX", nChar) converts a decimal to hexadecimal just fine!
0 Kudos
ThisIsEd
Level 4

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;
//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;
0 Kudos
MichaelU
Level 12 Flexeran
Level 12 Flexeran

Overall that sounds good. One minor thing: you may want to use %02lX instead of %lX in order to properly handle values below 0x10.
0 Kudos