DataPower

 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 17 days ago

    Dear Steve ,

    I have same requirement , I need to send to backend as "application/x-www-form-urlencoded" format  with two value "grant_type" and "assertion" , but I am not able to read those parameter using wither xslt and gateway script . Can you please help me on this . This is my gateway script 

    // use the urlopen module
    var urlopen = require ('urlopen');
    var apim = require ('apim');
    var hm = require('header-metadata');
    var sm = require('service-metadata');
    var query = require('querystring')
    var list = sm.list();
    var fullHeaders = hm.current.get();
    // Retrieve a json object with all headers in another form
    var fullHeaders = hm.current.headers;
    console.debug("Full Header : "+JSON.stringify(fullHeaders));
    console.debug("Full Service Data : "+JSON.stringify(list));
    console.debug("Protocol Method: "+sm.protocolMethod);
    console.debug("Protocol URL: "+sm.getVar('var://service/URL-in'));
    // define the urlopen options
    var options = {
        //target: "https://xxxxxxxx/authz/v1/oauth2/token",
        // if target is https, supply a sslProxyProfile
    target: "https://xxxxxxx"+sm.URI,
        sslClientProfile: "thales-client-dp-profile",
        method: sm.protocolMethod,
        //headers: fullHeaders,
    accept: 'application/json',
    contentType: 'application/x-www-form-urlencoded',
    xcorrelationId: '45678210',
        timeout: 60,
        //data: apim.getvariable('request.body')
    data: "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=<need the value from the request>"
    };
    session.output.write("Request Body: "+options);
    console.debug("Request Body: "+JSON.stringify(options));
    // 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;
    var responseReasonPhrase = response.reasonPhrase;
    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: responseReasonPhrase,
    });           
            }
        }
    }); 
    apim.output('application/json');


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