IBM TechXchange Group

TechXchange Group

Your hub for all things community! Ask questions, connect with fellow members, get the support you need, and stay informed with the latest updates.

 View Only

Call Backend API using Data power Gateway script : Using urlopen.open

  • 1.  Call Backend API using Data power Gateway script : Using urlopen.open

    Posted Mon July 08, 2024 06:51 PM

    Dear Community Team,

    Need your support . 

    My requirement is call the backend service using "application/x-www-form-urlencoded"  . But my  requirement is send header and data dynamically , whatever send from the request channel . I suspect that my gateway script not able to send the payload in data and header , that's why i am always getting 403

    Example Curl : curl --request POST \
      --url https://xxxxxxxxx/authz/v1/oauth2/token \
      --header 'Accept: application/json' \
      --header 'Content-Type: application/x-www-form-urlencoded' \
      --header 'x-correlation-id: ' \
      --data grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer \
      --data assertion='JWTToken reived from channel'

    My Gateway Script :

    // use the urlopen module
    var urlopen = require ('urlopen');
    var apim = require ('apim');
    // define the urlopen options
    var options = {
        target: "https://xxxxxxx/authz/v1/oauth2/token",
        // if target is https, supply a sslProxyProfile
        sslClientProfile: "thales-mutual-authrntication",
        method: "POST",
        headers: apim.getvariable('request.headers'),
        contentType: 'application/x-www-form-urlencoded',
        timeout: 60,
        data: apim.getvariable('request.body')
    };
    // 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
    console.debug("urlopen connect error: " +JSON.stringify(error));
            session.output.write ("urlopen connect error: " + JSON.stringify(error));
        } else {
            // read response data
            // get the response status code
            var responseStatusCode = response.statusCode;
    console.debug("responseStatusCode: " +responseStatusCode);
            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 {
    var hm = require('header-metadata');
    var contentType = hm.current.get('content-type');
    // Retrieve a json object with all headers
    var allHeaders = hm.current.get();
    // Retrieve a json object with all headers in another form
    var allHeaders = hm.current.headers;
    session.output.write({
    code: responseStatusCode,
    message: 'The request is understood, but it has been refused or access is not allowed',
    });           
            }
        }
    }); // end of urlopen.open()
    apim.output('application/json');



    ------------------------------
    Anil Nayak
    ------------------------------