
edrabbit1.552491848548566E12 asked a question.
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!
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.
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;