API Connect

 View Only
  • 1.  encodeURIComponent() and .replace('&', '%26') is not supported in DataPower

    Posted Fri May 13, 2022 09:49 AM
    Hi Team,

    We trying to use the encodeURIComponent('oauth_hash=kDGUqHA/x0By=&oauth_key=7K3VP3nfc62646c!dbac363f4') and .replace('&', '%26').replace('/', '%2F').replace('=',"%3D").replace(':', "%3A");
    above both are not working it is generating a "NaN" attached string like "auth_hash=kDGUqHA/x0By= NaNauth_consumer_key=7K3VP3nfc62646c!dbac363f4"

    How to get Achieve this? 

    Below link also not working in my scenario and this is implemented in xslt code through if it is available in Gateway Script it helps a lot.
    Link: Processing UTF-8 URL-encoded URIs with WebSphere DataPower

    Thanks & Regards,
    Nagababu K


    ------------------------------
    kandula nagababu
    ------------------------------


  • 2.  RE: encodeURIComponent() and .replace('&', '%26') is not supported in DataPower

    Posted Tue May 17, 2022 08:47 AM

    Hi Kandula,

    I ran your encodeURIComponent command above in the GatewayScript debugger

    (debug) p encodeURIComponent('oauth_hash=kDGUqHA/x0By=&oauth_key=7K3VP3nfc62646c!dbac363f4')
    oauth_hash%3DkDGUqHA%2Fx0By%3D%26oauth_key%3D7K3VP3nfc62646c!dbac363f4

    which is properly encoding the &, /, = characters.  Your example doesn't have a colon, but 

    (debug) p encodeURIComponent(':')
    %3A

    so that is working too, so your NaN is not coming from the encodeURIComponent function.  Since your string is a query string, do you really want to escape the = and & characters that distinguish the name=value pairs of the querstring and separate different pairs?  For sure characters in the values should be encoded.  Perhaps you should use the querystring module?

    (debug) p querystring.parse('oauth_hash=kDGUqHA/x0By=&oauth_key=7K3VP3nfc62646c!dbac363f4')
    { oauth_hash: 'kDGUqHA/x0By=', oauth_key: '7K3VP3nfc62646c!dbac363f4' }
    (debug) p querystring.stringify(querystring.parse('oauth_hash=kDGUqHA/x0By=&oauth_key=7K3VP3nfc62646c!dbac363f4'))
    oauth_hash=kDGUqHA%2Fx0By%3D&oauth_key=7K3VP3nfc62646c!dbac363f4

    The parse function simply parses the query string into a JSON object and decodes and escaped characters in the values, which in your case makes no change to the values, but the stringify will place this back into a query string which encodes the proper characters in the values.

    I'm not fully sure what your requirement is, so perhaps more of your code would assist to understanding where the NaN is coming from if the above doesn't meet your requirement.

    Regards,

    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 3.  RE: encodeURIComponent() and .replace('&', '%26') is not supported in DataPower

    Posted Wed May 18, 2022 09:10 AM

    Hi Kandula,

    I received your reply although I'm not sure why it is not showing here.  You indicated you were using DataPower 2018.4.1 to see these results.  Please advise what specific build your are using.  I took the code you sent and ran it through the GatewayScript debugger on 2018.4.1.20 and I'm not seeing your results.

    Version: IDG.2018.4.1.20 build rel-2018-4-1-branch.342592 on Apr 19, 2022 4:51:22 AM
    Delivery type: LTS
    Serial number: 0000000

    3:function getSignatureBaseString(httpMethod, baseUri, paramString) {
    =>4: debugger;
    5: let encodeParamsString = encodeURIComponent(paramString);
    6: encodeParamsString = encodeParamsString.replace("*", "%2A");
    7:
    8: const abc=
    9: // Uppercase HTTP method
    10: `${httpMethod.toUpperCase()}&` +
    11: // Base URI
    12: `${encodeURIComponent(baseUri)}&` +
    13: // OAuth parameter string
    14: encodeParamsString;
    (debug) p httpMethod
    POST
    (debug) p baseUri
    https://sandbox.api.infotech.com/mdes/csapi/v2/token/delete
    (debug) p paramString
    oauth_body_hash=kDGUqHA8UxDlHnRS9ox8AfEGID/x0BybWWqFde6KLR0=&oauth_nonce=U66a9vuB&oauth_timestamp=1652377747&oauth_version=1.0

    > the above are your input arguments to your function
    (debug) n
    5: let encodeParamsString = encodeURIComponent(paramString);
    =>6: encodeParamsString = encodeParamsString.replace("*", "%2A");
    (debug) p encodeParamsString
    oauth_body_hash%3DkDGUqHA8UxDlHnRS9ox8AfEGID%2Fx0BybWWqFde6KLR0%3D%26oauth_nonce%3DU66a9vuB%26oauth_timestamp%3D1652377747%26oauth_version%3D1.0
    > the encodeURIComponent function did not inject a NaN
    (debug) n
    8: const abc=
    9: // Uppercase HTTP method
    =>10: `${httpMethod.toUpperCase()}&` +
    11: // Base URI
    12: `${encodeURIComponent(baseUri)}&` +
    13: // OAuth parameter string
    14: encodeParamsString;
    (debug) p encodeParamsString
    oauth_body_hash%3DkDGUqHA8UxDlHnRS9ox8AfEGID%2Fx0BybWWqFde6KLR0%3D%26oauth_nonce%3DU66a9vuB%26oauth_timestamp%3D1652377747%26oauth_version%3D1.0

    > Replacing an * with an url encoded char did nothing as there are no * in the variable to replace
    (debug) n
    =>17: return abc.replace(/!/, "%21");
    (debug) p abc.replace(/!/, "%21")
    POST&https%3A%2F%2Fsandbox.api.infotech.com%2Fmdes%2Fcsapi%2Fv2%2Ftoken%2Fdelete&oauth_body_hash%3DkDGUqHA8UxDlHnRS9ox8AfEGID%2Fx0BybWWqFde6KLR0%3D%26oauth_nonce%3DU66a9vuB%26oauth_timestamp%3D1652377747%26oauth_version%3D1.0

    expected: POST&https%3A%2F%2Fsandbox.api.infotech.com%2Fmdes%2Fcsapi%2Fv2%2Ftoken%2Fdelete&oauth_body_hash%3DkDGUqHA8UxDlHnRS9ox8AfEGID%2Fx0BybWWqFde6KLR0%3D%26oauth_nonce%3D05f4de9c56aa6f60%26oauth_timestamp%3D1651672433%26oauth_version%3D1.0

    > There was no ! to replace either so no change there.  Other than some differences in the input data which would have some differences in the expected result in the comment, I'm not seeing an issue.

    I'm using the latest 2018.4.1.20 fixpack.  If there was an issue in a previous fixpack, just this test would tell me that it has been resolved with the latest fixpack. 

    Regards,
    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 4.  RE: encodeURIComponent() and .replace('&', '%26') is not supported in DataPower

    Posted Sat May 21, 2022 06:42 AM
    Hi Steve Linn,

    Thank you so much for your valuable information.

    It's working fine for me after applying the fix pack.

    ------------------------------
    kandula nagababu
    ------------------------------