DataPower

DataPower

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
  • 1.  Best way to validate x fields in query strings

    Posted Mon July 20, 2020 05:47 AM
    What is the best solution to validate if values are being passed in a query string URL?

    For example:

    I want to validate if values DATE and TIME are being provided in the URL, only these 2 params are required

    https://test.com?DATE=2020-02-02&TIME=25:55

    I´m thinking of getting the URL variable via XSLT but it would required a lot of work, I would need to check if we have only 2 params and if the 2 params are the ones above. Is there an alternative?

    Thanks!

    ------------------------------
    Leandro Takeda
    ------------------------------


  • 2.  RE: Best way to validate x fields in query strings

    Posted Tue July 21, 2020 10:03 AM
    Use convert-http action ("Convert Query params"), that will convert the URL into parts in XML, that can be addressed easily.

    ------------------------------
    Hermann Stamm-Wilbrandt
    Compiler Level 3 support & Fixpack team lead
    IBM DataPower Gateways (⬚ᵈᵃᵗᵃ / ⣏⠆⡮⡆⢹⠁⡮⡆⡯⠂⢎⠆⡧⡇⣟⡃⡿⡃)
    https://stamm-wilbrandt.de/en/blog/
    ------------------------------



  • 3.  RE: Best way to validate x fields in query strings

    Posted Tue July 21, 2020 05:12 PM

    I see Hermann already replied on the Convert Query Params DataPower action, but if you're partial to GatewayScript, you can use the url module.  Here's an example:

    let testurl = 'https://host:443/mypath?p1=v1&p2=v2';
    let url = require('url');
    let urlObj = url.parse(testurl,true);

    The urlObj object after the parse is shown by my GatewayScript debugger output:


    (debug) p urlObj
    { protocol: 'https:',
    slashes: true,
    auth: null,
    host: 'host:443',
    port: '443',
    hostname: 'host',
    hash: null,
    search: '?p1=v1&p2=v2',
    query: { p1: 'v1', p2: 'v2' },
    pathname: '/mypath',
    path: '/mypath?p1=v1&p2=v2',
    href: 'https://host:443/mypath?p1=v1&p2=v2' }

    Since you have the query property (because the 2nd argument to parse was true), you can not only count the number of Object.keys in urlObj.query, but you can iterate over them to determine if they're the ones you want.  To reject the request if it doesn't have the query parameters you expect, you can do a

    session.reject('some error message');

    Regards,

    Steve