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
Expand all | Collapse all

Cache backend response to POST calls

  • 1.  Cache backend response to POST calls

    Posted Thu November 19, 2020 08:57 AM

    Hi Team,

    I have below requriement

    I have 3 MPGW services , and these 3 services are calling the same backend.Now my requirement is isntead of calling the backend everytime in all 3 services, I wanted to call the backend in 1st MPG and cache that response and pass it to the 2nd and 3rd MPG services.

    For this I have created common xml manager and using the same xml manager object in all 3 MPG services.I am getting the response from the backend in 1st MPG, but couldn't able to cache the response. When verified document cache status it is not caching anything.

    Configured below things in xml manager

    Under the "Document Cache Policy" tab

    URL Match = *

    PolicyType =Fixed

    TTL =86400

    Priority = 128

    Cache grid =NA

    Cache backedn response = ON

    HTTP Cache Validation = on

    Return Expired Document = On

    RESTFul Invalidation =Off

    Cache Response to POST and PUT request = On

    In my 1st MPGW service Req message is SOAP converting to json message and that json message is input to the below js file , whatever the response coming from the below js file need cache that response and have to pass it to other two MPG services.Below is the sample code from the js file and reading backend url, client profile from the MPGW stylesheet params

    Please help me, do I need to add any extra configuration to cache the response for POST calls.

    MPG Service flow

    SOAP---> Converting to json---> Backend(json message)

    session.input.readAsJSON(function(readAsJSONError, incomingdata) {

    if (readAsJSONError) {

    session.output.write(readAsJSONError);

    } else {

    //var request = session.createContext('InputRequest');

    //request.setVar('InputRequest', incomingdata);

    var ctx = session.name("myContext")|| session.createContext("myContext");

    var id = ctx.getVar("diaplayId")

    var payload={

    "ID_": id

    }

    ctx.setVariable("InputRequest", payload);

    var EncodedCred = 'Basic '+new Buffer(properties.Authorization).toString('base64');

    ctx.setVariable('EncodedCred', EncodedCred);

    console.log("properties" + JSON.stringify(properties));

    //open call

    var api = {

    target: uri,

    sslClientProfile: 'test_SSL_ClientProfile',

    method: 'post',

    contentType: 'application/json',

    headers: {

    Authorization : EncodedCred

    },

    data: payload

    };

    ctx.setVariable('backendurl', properties.target);

    ctx.setVariable('sslprofile', properties.sslClientProfile);

    ctx.setVariable('api', api);

    // open connection to target and send data over

    urlopen.open(api, function(error, response) {

    if (error) {

    // an error occurred during request sending or response header parsing

    console.log("error while calling api " + JSON.stringify(error));

    hm.response.statusCode = responseStatusCode;

    session.output.write(error);

    } else {

    var responseStatusCode = response.statusCode;

    if (responseStatusCode == 200) {

    response.readAsJSON(function(error, responseData) {

    if (error) {

    // error while reading response or transferring data to Buffer

    logger.error("failure in putting message to api" + JSON.stringify(error));

    } else {

    hm.response.statusCode = '200 Message is received';

    session.output.write(responseData);

    logger.debug("response received from uri call:" + responseData);

    var backendResponse = responseData;

    var ctx = session.name("myContext")|| session.createContext("myContext");

    ctx.setVariable("backendResponse", backendResponse);

    var name = backendResponse.test.name;

    ctx.setVariable("name", name)

    }

    });

    } else {

    logger.error("Error returned from uri"+" : with statusCode " + responseStatusCode);

    throw error();

    }

    }

    }); // end of urlopen.open()

    }

    });



    #DataPower
    #Support
    #SupportMigration


  • 2.  RE: Cache backend response to POST calls

    Posted Fri November 20, 2020 07:35 AM

    Hi,

    did you enable the document cache by setting non-zero value to "Document Cache Size" on XML manager Document cache tab? You also have to set x-dp-cache-key HTTP header as described in the link below:

    https://www.ibm.com/support/pages/ibm-datapower-document-cache-cache-response-content

    --HP



    #DataPower
    #Support
    #SupportMigration