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.  urlopen module in datapower gateway script

    Posted Tue November 29, 2022 03:28 PM
    I am new to Datapower and working with an existing gateway script using urlopen module to connect to a server and fetch response using GET method. 
    The target server returns response headers and a response body. I am missing the response body in the received object.

    Below is the sample:

    function urlOpen(urlopenOptions) {
    return new Promise(function (resolve, reject) {
    urlopen.open(urlopenOptions, function (error, response) {
    if (error) {
    resolve(reject);
    } else {
    console.log(response);
    resolve(response);
    }
    });
    });
    }

    ------------------------------
    Alok Kumar Pandey
    ------------------------------


  • 2.  RE: urlopen module in datapower gateway script

    Posted Tue November 29, 2022 05:20 PM

    Hi Alok,

    The response body is in the response object.  Within the urlopen.open async function you need to do a response.readAs[Buffer|XML|JSON] to get access to the payload.  response.statusCode will have the status code.  See the examples in https://www.ibm.com/docs/en/datapower-gateway/10.0.1?topic=apis-urlopen-module.

    Best Regards,
    Steve Linn



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



  • 3.  RE: urlopen module in datapower gateway script

    Posted Wed November 30, 2022 11:14 AM
    Hi Alok,

    Adjunto un ejemplo:

    ============================================================================================

    //import module urlopen
    const urlopen = require('urlopen');

    //import module apim
    var apim = require('apim');

    const options = {
    target : 'https://reqres.in/api/users/1',
    method : "get",
    contentType : "application/json",
    sslClientProfile: 'api-sslcli-all',
    timeout : 60
    };

    // open connection to target and send data over
    urlopen.open (options, function (error, response) {
    if (error) {
    // an error occurred during request sending or response header parsing
    session.output.write ("urlopen connect error: " + JSON.stringify(error));
    } else {
    // read response data
    // get the response status code
    var responseStatusCode = response.statusCode;
    if (responseStatusCode == 200) {
    response.readAsBuffer(function(error, responseData) {
    if (error) {
    // error while reading response or transferring data to Buffer
    session.output.write("readAsBuffer error: " + JSON.stringify(error));
    } else {
    session.output.write(responseData);
    }
    });
    } else {
    session.output.write ("urlopen target return statusCode " + responseStatusCode);
    }
    }
    }); // end of urlopen.open()

    apim.output('application/json');

    ============================================================================================

    ------------------------------
    Julio Pari
    IT Architect IBM
    Bank
    ------------------------------



  • 4.  RE: urlopen module in datapower gateway script

    Posted Wed November 30, 2022 11:14 AM
      |   view attached
    Hi, adjunto example


    ------------------------------
    Julio Pari
    IT Architect IBM
    Bank
    ------------------------------

    Attachment(s)

    txt
    gatewayscript-4.txt   6 KB 1 version


  • 5.  RE: urlopen module in datapower gateway script

    Posted Mon January 23, 2023 03:58 PM
    Thanks your solution, it had worked.
    Would it be possible to help with another questions.

    https://community.ibm.com/community/user/integration/discussion/api-for-fetching-list-of-scopes-defined-in-the-oauth-provider?ReturnUrl=%2fcommunity%2fuser%2fintegration%2fcommunities%2fcommunity-home%2fdigestviewer%3fcommunitykey%3db13f4693-16ee-422b-9d0b-f5262e25426f

    ------------------------------
    Alok Kumar Pandey
    ------------------------------