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.


#Other
#TechXchangePresenter

 View Only
Expand all | Collapse all

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



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

    Posted Thu July 03, 2025 04:38 PM

    Hi Anil...

    Been on vacation so did not see your question...

    A few things.. The 403 you are getting could mean many things :) Can you see that on the target server?

    Another thing is the  data: apim.getvariable('request.body').. The request.body might not be available. Depends on the script (is it in an assembly or an API) - if it is in an API you need to apply correct configuration just like with payload logging - or it will be "streamed" through and not available. If it is in an assembly script you could read it by doing something like.. (simple example) it is all async - so has to be done at the end of you assembly..

    context.request.body.readAsBuffer (function (error, bodybuffer) {
      if (error) {
          throw 'Something something';
      }
      else  {
         // DO stuff with data (bodybuffer) if you need
    	 
    	 // Now call the backend
    	 
         urllopen.open(options, function(error, response) {
         if (error) {
            throw 'Something something';
         }
         else {
    	   // Handle response and stuff
         }
      }
    });
    
    

     



    ------------------------------
    Jesper Nielsen
    Architect
    BEC Financial Technologies a.m.b.a.
    Roskilde
    ------------------------------