Loading
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!

  • 0_M Urman (Flexera Software)

    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.
  • 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.
    Expand Post
  • 0_M Urman (Flexera Software)

    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).
  • 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?
  • 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;
    Expand Post
  • 0_M Urman (Flexera Software)

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

Loading
URLEncode a string in InstallScript