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

how to print json format using style sheet

Community Support Admin

Community Support AdminThu October 01, 2020 09:17 AM

Community Support Admin

Community Support AdminThu October 01, 2020 10:53 AM

  • 1.  how to print json format using style sheet

    Posted Mon September 28, 2020 11:10 AM

    hello,

    allow me to explain my case , I have mpgw with json in input and json as output

    I checked the json http header named by " id " in case matches, the message will be passed to the provider while if its not match json response will be printed and request will be rejected. try to test the second scenario and then I got rejected when it's not match but the response is client error as xml

    the provided stylesheet is included below

    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dp="http://www.datapower.com/extensions" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">

    <xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:param name="ID" select="3"/>

    <xsl:variable name="id" select="dp:http-request-header('ID')"/>

    <xsl:template match="/">

    <xsl:variable name="match" select="ID=$id"/>

    <xsl:choose>

    <xsl:when test="$match">

    <MESSAGE>Pass</MESSAGE>

    </xsl:when>

    <xsl:otherwise>

    <json:object>

    <json:string name="Description">Unauthorized</json:string>

    </json:object>

    <dp:reject/>

    </xsl:otherwise>

    </xsl:choose>

    </xsl:template>

    </xsl:stylesheet>

    output is

    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">

    <env:Body>

    <env:Fault>

    <faultcode>env:Client</faultcode>

    <faultstring>(from client)</faultstring>

    </env:Fault>

    </env:Body>

    </env:Envelope>

    Transformation action reads from context var and output to null



    #DataPower
    #Support
    #SupportMigration


  • 2.  RE: how to print json format using style sheet

    Posted Tue September 29, 2020 07:47 AM

    hello

    try this

    <?xml version="1.0" encoding="UTF-8"?>

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xmlns:dp="http://www.datapower.com/extensions" extension-element-prefixes="dp" exclude-result-prefixes="#all">

    <xsl:variable name="id" select="3"/>

    <xsl:template match="/">

    <xsl:choose>

    <xsl:when test="dp:http-request-header('ID') = $id">

    <MESSAGE>Pass</MESSAGE>

    </xsl:when>

    <xsl:otherwise>

    <!--<json:object>

    <json:string name="Description">Unauthorized</json:string>

    </json:object>

    <dp:reject/>-->

    <xsl:text>{"Description":"Unauthorized"}</xsl:text>

    <dp:set-variable name="'var://service/mpgw/skip-backside'" value="'1'"/>

    <dp:set-http-request-header name="'Content-Type'" value="'application/json'"/>

    </xsl:otherwise>

    </xsl:choose>

    </xsl:template>

    </xsl:stylesheet>



    #DataPower
    #Support
    #SupportMigration


  • 3.  RE: how to print json format using style sheet

    Posted Tue September 29, 2020 08:51 AM

    I got

    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">

      <env:Body>

       <env:Fault>

         <faultcode>env:Client</faultcode>

         <faultstring>[{"Description":"Unauthorized"}] (from client)</faultstring>

       </env:Fault>

      </env:Body>

    </env:Envelope>



    #DataPower
    #Support
    #SupportMigration


  • 4.  RE: how to print json format using style sheet

    Posted Tue September 29, 2020 10:16 AM

    Are you sur you removed <dp:reject/> from the xslt ?



    #DataPower
    #Support
    #SupportMigration


  • 5.  RE: how to print json format using style sheet

    Posted Tue September 29, 2020 10:17 AM

    Are you sur you removed <dp:reject/> from the xslt ?​



    #DataPower
    #Support
    #SupportMigration


  • 6.  RE: how to print json format using style sheet

    Posted Tue September 29, 2020 10:51 AM

    it's mandatory in case if user is unauthorized the request is rejected and error rule is triggered

    is there any other way i can trigger error rule without dp:reject ?



    #DataPower
    #Support
    #SupportMigration


  • 7.  RE: how to print json format using style sheet

    Posted Tue September 29, 2020 11:07 AM

    ​If you explain exactly what you need, i think i can help you.


    What i understood 

    1. if id http request header is different than 3, you want to generate this json {"Description":"Unauthorized"} and not call the backEnd
    2.  if id http request header is equal 3, you want to pass the input json to the backEnd


    Is that true ?



    #DataPower
    #Support
    #SupportMigration


  • 8.  RE: how to print json format using style sheet

    Posted Tue September 29, 2020 12:42 PM

    yes true. the request will be rejected and error rule will be executed . in that error rule I should print the json response {"Description":"Unauthorized"} 



    #DataPower
    #Support
    #SupportMigration


  • 9.  RE: how to print json format using style sheet

    Posted Tue September 29, 2020 12:59 PM

    my last stylesheet do what you want.


    when you use skip-backside = 1, the datapower does not send the request to the back end. This Policy became a loopBack call.

    So that's the Reason i create a json message in this case


    So, try to use my stylesheet with no modification.



    #DataPower
    #Support
    #SupportMigration


  • 10.  RE: how to print json format using style sheet

    Posted Tue September 29, 2020 04:02 PM

    thank you so much for your effore David

    when I tried it without dp:reject the request rule will execute the next action which is intended to build the backend request , any idea ?



    #DataPower
    #Support
    #SupportMigration


  • 11.  RE: how to print json format using style sheet

    Posted Wed September 30, 2020 06:50 AM

    Ah ok.

    You didn't tell me ​there are other actions next styleSheet node.

    In this cas, use dp:reject in my stylesheet and create and error rule Policy to generate a json



    #DataPower
    #Support
    #SupportMigration


  • 12.  RE: how to print json format using style sheet

    Posted Wed September 30, 2020 07:51 AM

    yes I did that in error rule there will be xsl transformation action to print json response since the input is in json xslt will not accept it as xsl only accept xml in input, i have used convert query parameter to xml before building json response action but I got that json is invalid in ## offset , any help to do that ?



    #DataPower
    #Support
    #SupportMigration


  • 13.  RE: how to print json format using style sheet

    Posted Thu October 01, 2020 08:56 AM

    you're right when using "convert query parameter". Because of that, you can execute an xslt stylesheet.

    What response do you want exactly ?

    This json below ?

    {"Description":"Unauthorized"}



    #DataPower
    #Support
    #SupportMigration


  • 14.  RE: how to print json format using style sheet

    Posted Thu October 01, 2020 09:17 AM


  • 15.  RE: how to print json format using style sheet

    Posted Thu October 01, 2020 09:20 AM

    <?xml version="1.0" encoding="UTF-8"?>

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dp="http://www.datapower.com/extensions" extension-element-prefixes="dp" exclude-result-prefixes="#all" xmlns:dpconfig="http://www.datapower.com/param/config">

    <xsl:output omit-xml-declaration="yes" media-type="application/json" method="text"/>

    <xsl:template match="/">

    <xsl:text disable-output-escaping="yes">{"Description":"Unauthorized"}</xsl:text>

    </xsl:template>

    </xsl:stylesheet>



    #DataPower
    #Support
    #SupportMigration


  • 16.  RE: how to print json format using style sheet

    Posted Thu October 01, 2020 09:22 AM

    create this transform action after your "convert query parameter"​ and copy my styleSheet



    #DataPower
    #Support
    #SupportMigration


  • 17.  RE: how to print json format using style sheet

    Posted Thu October 01, 2020 10:48 AM

    I did

    it did not pass " convert query parameter ", it says "Unable to parse JSON and generate JSONx: Incomplete markup or missing document element at offset 39 of https://IP:PORT/"

    looks some playground variable was behind this error cause the same json input that has entered request rule. how can we print the json response using Gateway script along with setting http status code ? looks it will be more suitable.



    #DataPower
    #Support
    #SupportMigration


  • 18.  RE: how to print json format using style sheet

    Posted Thu October 01, 2020 10:48 AM

    I did

    it did not pass " convert query parameter ", it says "Unable to parse JSON and generate JSONx: Incomplete markup or missing document element at offset 39 of https://IP:PORT/"

    looks some playground variable was behind this error cause the same json input that has entered request rule. how can we print the json response using Gateway script along with setting http status code ? looks it will be more suitable.



    #DataPower
    #Support
    #SupportMigration


  • 19.  RE: how to print json format using style sheet

    Posted Thu October 01, 2020 10:52 AM

    Remove the "convert params to xml" action and put 'NULL' into transform action input context​



    #DataPower
    #Support
    #SupportMigration


  • 20.  RE: how to print json format using style sheet

    Posted Thu October 01, 2020 10:53 AM

    export your MPG and export it​



    #DataPower
    #Support
    #SupportMigration


  • 21.  RE: how to print json format using style sheet

    Posted Thu October 01, 2020 11:08 AM

    if you prefer, here is the Gateway script


    session.input.readAsBuffers (function (error, bufs) {

    var headers = require('header-metadata');

    headers.current.set('Content-Type','application/json;charset=utf-8');

      session.output.write('{"Description":"Unauthorized"}');

    });



    #DataPower
    #Support
    #SupportMigration


  • 22.  RE: how to print json format using style sheet

    Posted Thu October 01, 2020 11:39 AM

    it worked THANKS DAVID !

    one last thing, how can I do if statement in JS ?

    I have created context variable in request rule in case authorization fails, the context variable will have " Unauthorized " , I'd check that in js if context var has Unauthorized word then it will print

      session.output.write('{"Description":"Unauthorized"}'); else it will print

      session.output.write('{"Description":" Error"}');




    #DataPower
    #Support
    #SupportMigration


  • 23.  RE: how to print json format using style sheet

    Posted Thu October 01, 2020 12:39 PM

    If your context variable is called, for example, var://context/TEST/VALUE​ and contains 'true' or 'false', you can test this script


    session.input.readAsBuffers (function (error, bufs) {

    var headers = require('header-metadata');

    headers.current.set('Content-Type','application/json;charset=utf-8');

    if ( session.name('TEST').getVar('VALUE') == 'true') {

    session.output.write('{"Description":"Unauthorized"}');

    } else {

    session.output.write('{"Description":"authorized"}');

    }

     

    });




    #DataPower
    #Support
    #SupportMigration