Layer7 API Management

Expand all | Collapse all

Javascript Assertion

  • 1.  Javascript Assertion

    Posted 02-05-2019 08:44 AM

    I have a use case to extract response body in javascript assertion. I am able to get the response variable as

    context.getVariable("response"). But i was not sure how to extract body part/mainpart from that.

    I tried context.getVariable("response.mainpart"), but is not working. Can someone help me in this?



  • 2.  Re: Javascript Assertion

    Posted 02-05-2019 09:03 AM
      |   view attached

    Hi Giridharan,

     

    I have attached a sample here. Please let me know if this helps.

     

    Regards,

    Joe

    Attachment(s)



  • 3.  Re: Javascript Assertion

    Posted 02-06-2019 02:22 AM

    Hi Joe,

           I am able to do the same with default response object. But in my use case i have set the response into a new context variable say httpResponse1, there am not able to access the mainpart as context.getVariable("httpResponse1.mainpart")



  • 4.  Re: Javascript Assertion

    Posted 02-06-2019 05:09 PM

    as per the document Execute JavaScript Assertion - CA API Gateway - 9.4 - CA Technologies Documentation , access the variable and message are different.

    here is an example to get message body,

    var resp = context.getOrCreateMessageVariable ('httpresp1');
    var respbody = resp.getContent();
    var myrespbody = 'response:' + respbody;

    context.setVariable('myresp', myrespbody);


  • 5.  Re: Javascript Assertion

    Posted 02-07-2019 02:17 AM

    Exactly What I am looking for. But when i tried to log the respbody defined as above example, it is showing as [ object Object ]. How to convert or parse it to string and log it?



  • 6.  Re: Javascript Assertion

    Posted 02-07-2019 06:33 PM

    Okay, it seems the respbody only be a string if the content-type is text/plain.

    JSON.stringify(respbody) should be fine to most of content-type except application/octet-stream

    (for xml body, it will be converted to json)

     

    I think it could be worth an idea ticket to ask product team to add getTextContent(), or toString() for interface JavaScriptMessage

     

    Currently, we can get the message body as string by following steps.

    In policy,

    Set Context Variable textBody as String to: ${httpresp1.mainpart}

    before javascript assertion, and in javascript assertion

    var respbody = context.getVariable ('textBody');