DataPower

 View Only
Expand all | Collapse all

Call Backend API using Data power Gateway script : Using "urlopen.open" Without manipulating Request

  • 1.  Call Backend API using Data power Gateway script : Using "urlopen.open" Without manipulating Request

    Posted Wed July 10, 2024 03:58 AM
      |   view attached

    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
    ------------------------------


  • 2.  RE: Call Backend API using Data power Gateway script : Using "urlopen.open" Without manipulating Request

    Posted Fri July 19, 2024 09:05 AM

    Hi Anil,
    Since you're using var apim = require ('apim'); then this is a GatewayScript running within the API Connect API Gateway.  You have commented out data: apim.getvariable('request.body'), what was that returning to you? The apim module in the API Gateway returns v5 like responses, and if request.body is null in the API Gateway context, you would receive a empty XML nodelist.  This would be because of streaming of the inbound request such that the payload hasn't been stored into context.  You could have a parse policy prior to this GatewayScript which would consume the data from the stream and I would expect you'd now get the entire form post payload.  If you're passing the inbound request payload to your backend that should be sufficient.  If you only need to assertion form parameter, you could either parse the string yourself, but I believe a context.get('request.parameters.assertion') would allow you to get just that parameter.  Note that will return an object with two properties, a locations array which should have the one entry with a value of "form", and the other would be a values array where the first entry would be your value.  This structure is present in case you had the same named parameter in different locations (header, path, query, or form). 
    Best Regards,
    Steve Linn



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