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

Convert .zip file to base64 and send it to backend on Datapower

  • 1.  Convert .zip file to base64 and send it to backend on Datapower

    Posted Wed December 09, 2020 01:34 PM

    Dears,

    I have requirement where i have to fetch .zip file from sftp server and convert it to base64 and send it to backend.

    we are able to fetch file from sftp but how to convert file to base64 and send to backend.

    Thanks



    #DataPower
    #Support
    #SupportMigration


  • 2.  RE: Convert .zip file to base64 and send it to backend on Datapower

    Posted Thu December 10, 2020 12:16 PM

    Hi,

    check Hermann's answer in the following thread:

    https://www.ibm.com/mysupport/s/question/0D50z00006Z5B7Z/encoded-zipped-xml-base64?language=en_US

    --HP



    #DataPower
    #Support
    #SupportMigration


  • 3.  RE: Convert .zip file to base64 and send it to backend on Datapower

    Posted Thu December 10, 2020 12:59 PM

    Hi hpernaa ,

    We are able to pull a file from sftp from sftp poller but unable to convert base 64 using xslt in data power or action .

    Regards ,

    Sanket .



    #DataPower
    #Support
    #SupportMigration


  • 4.  RE: Convert .zip file to base64 and send it to backend on Datapower

    Posted Fri December 11, 2020 01:53 PM

    Hello

    How did you get the file from sftp ? And how do you want to send to backEnd ? with dp:url-open into a styleSheet ? Or direct with a result action ?



    #DataPower
    #Support
    #SupportMigration


  • 5.  RE: Convert .zip file to base64 and send it to backend on Datapower

    Posted Fri December 11, 2020 02:42 PM

    Hi David ,

    I am able pull a file from sftp poller and need send as base 64 encoded file to backend using dp:url-open into a styleSheet.

    Regards ,

    sanket



    #DataPower
    #Support
    #SupportMigration


  • 6.  RE: Convert .zip file to base64 and send it to backend on Datapower

    Posted Mon December 14, 2020 05:34 AM

    Hi ,

    Is there any solution for this .

    Regards ,

    San



    #DataPower
    #Support
    #SupportMigration


  • 7.  RE: Convert .zip file to base64 and send it to backend on Datapower

    Posted Mon December 14, 2020 03:56 PM

    Yes, there is a solution

    Please can you telle me Something :

    •  Do you need to extract the file from the zip,  then encode it to base64, then send it to your backEnd ?
    • do you need to encode the zip in base-64 ​and send it to the backEnd ?


    #DataPower
    #Support
    #SupportMigration


  • 8.  RE: Convert .zip file to base64 and send it to backend on Datapower

    Posted Mon December 14, 2020 04:55 PM

    Hi David ,

    There will be 2 requirement where 2nd requirement i have done and where in 1st i need to encode the zip file( which is pull by sftp puller ) in base-64 ​and send it to the backend ?


    Requrirement 1:- pull a file from sftp ,convert to base64 and call a backend service .


    Requrirement 2:- pull a file from sftp and push to another sftp server .

    ===========================================

    Requrirement 2 xslt code :-

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

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    xmlns:dp="http://www.datapower.com/extensions"

    xmlns:regexp="http://exslt.org/regular-expressions" extension-element-prefixes="dp regexp"

    exclude-result-prefixes="dp regexp" version="1.0">

    <xsl:template match="/">

    <xsl:variable name="FrontsideFTP" select="dp:variable('var://service/URL-in')"/>

    <xsl:variable name="Filename" select="regexp:match($FrontsideFTP, '([^\/]*)$')"/>

    <xsl:variable name="Filename2" select="substring-before($Filename,'?Rename')"/>

    <xsl:variable name="BacksideFTP" select="concat('sftp://admin:Passxxxx/home/admin/File/FTP/',$Filename2)"/>

    <dp:set-variable name="'var://service/routing-url'" value="$BacksideFTP"/>

    <xsl:message dp:priority="debug">OId URL: <xsl:value-of select="$FrontsideFTP"/></xsl:message>

    <xsl:message dp:priority="debug">Filename: <xsl:value-of select="$Filename"/></xsl:message>

    <xsl:message dp:priority="debug">Filename2: <xsl:value-of select="$Filename2"/></xsl:message>

    <xsl:message dp:priority="debug">New URL: <xsl:value-of select="$BacksideFTP"/></xsl:message>

    <xsl:copy-of select ="*"/>

    <xsl:variable name="response">

    <dp:url-open target="{$BacksideFTP}" response="ignore" data-type="base-64">

    <xsl:copy-of select="Filename2"/>

    </dp:url-open>

    </xsl:variable>

    <xsl:message dp:priority="error">responseL: <xsl:value-of select="$response"/></xsl:message>

    <xsl:variable name="encdata" select="dp:binary-encode($Filename2)"/>

    <xsl:message dp:priority="error">encdata: <xsl:value-of select="$encdata"/></xsl:message>

    </xsl:template>

    </xsl:stylesheet>



    #DataPower
    #Support
    #SupportMigration


  • 9.  RE: Convert .zip file to base64 and send it to backend on Datapower

    Posted Mon December 14, 2020 04:58 PM

    Hi David ,

    There will be 2 requirement where 2nd requirement i have done and where in 1st i need to encode the zip file( which is pull by sftp puller ) in base-64 ​and send it to the backend ?


    Requrirement 1:- pull a file from sftp ,convert to base64 and call a backend service .


    Requrirement 2:- pull a file from sftp and push to another sftp server .

    ===========================================

    Requrirement 2 xslt code :-

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

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    xmlns:dp="http://www.datapower.com/extensions"

    xmlns:regexp="http://exslt.org/regular-expressions" extension-element-prefixes="dp regexp"

    exclude-result-prefixes="dp regexp" version="1.0">

    <xsl:template match="/">

    <xsl:variable name="FrontsideFTP" select="dp:variable('var://service/URL-in')"/>

    <xsl:variable name="Filename" select="regexp:match($FrontsideFTP, '([^\/]*)$')"/>

    <xsl:variable name="Filename2" select="substring-before($Filename,'?Rename')"/>

    <xsl:variable name="BacksideFTP" select="concat('sftp://admin:Passxxxx/home/admin/File/FTP/',$Filename2)"/>

    <dp:set-variable name="'var://service/routing-url'" value="$BacksideFTP"/>

    <xsl:message dp:priority="debug">OId URL: <xsl:value-of select="$FrontsideFTP"/></xsl:message>

    <xsl:message dp:priority="debug">Filename: <xsl:value-of select="$Filename"/></xsl:message>

    <xsl:message dp:priority="debug">Filename2: <xsl:value-of select="$Filename2"/></xsl:message>

    <xsl:message dp:priority="debug">New URL: <xsl:value-of select="$BacksideFTP"/></xsl:message>

    <xsl:copy-of select ="*"/>

    <xsl:variable name="response">

    <dp:url-open target="{$BacksideFTP}" response="ignore" data-type="base-64">

    <xsl:copy-of select="Filename2"/>

    </dp:url-open>

    </xsl:variable>

    <xsl:message dp:priority="error">responseL: <xsl:value-of select="$response"/></xsl:message>

    <xsl:variable name="encdata" select="dp:binary-encode($Filename2)"/>

    <xsl:message dp:priority="error">encdata: <xsl:value-of select="$encdata"/></xsl:message>

    </xsl:template>

    </xsl:stylesheet>

    • Like
    • Like
    • Answer
    • Reply
    • Select as Accepted answer




    #DataPower
    #Support
    #SupportMigration


  • 10.  RE: Convert .zip file to base64 and send it to backend on Datapower

    Posted Mon December 14, 2020 05:06 PM

    Can you explain me how you get the file from your sftp server ?



    #DataPower
    #Support
    #SupportMigration


  • 11.  RE: Convert .zip file to base64 and send it to backend on Datapower

    Posted Mon December 14, 2020 05:28 PM

    If you use a SFTP front side handler, you can use this styleSheet (you have to use Transform binary action)


    • This xslt below parse the input context into binary node and send it to the backend

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

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dp="http://www.datapower.com/extensions" xmlns:regexp="http://exslt.org/regular-expressions" extension-element-prefixes="dp regexp" exclude-result-prefixes="dp regexp" version="1.0">

    <dp:input-mapping href="store:///pkcs7-convert-input.ffd" type="ffd"/>

    <xsl:template match="/">

    <xsl:variable name="FrontsideFTP" select="dp:variable('var://service/URL-in')"/>

    <xsl:variable name="Filename" select="regexp:match($FrontsideFTP, '([^\/]*)$')"/>

    <xsl:variable name="Filename2" select="substring-before($Filename,'?Rename')"/>

    <xsl:variable name="BacksideFTP" select="concat('sftp://admin:Passxxxx/home/admin/File/FTP/',$Filename2)"/>

    <dp:set-variable name="'var://service/routing-url'" value="$BacksideFTP"/>

    <xsl:message dp:priority="debug">OId URL: <xsl:value-of select="$FrontsideFTP"/>

    </xsl:message>

    <xsl:message dp:priority="debug">Filename: <xsl:value-of select="$Filename"/>

    </xsl:message>

    <xsl:message dp:priority="debug">Filename2: <xsl:value-of select="$Filename2"/>

    </xsl:message>

    <xsl:message dp:priority="debug">New URL: <xsl:value-of select="$BacksideFTP"/>

    </xsl:message>

    <xsl:copy-of select="*"/>

    <xsl:variable name="response">

    <dp:url-open target="{$BacksideFTP}" response="ignore" data-type="base-64">

    <xsl:value-of select="dp:binary-encode(/object/message/node())"/>

    </dp:url-open>

    </xsl:variable>

    <xsl:message dp:priority="error">responseL: <xsl:value-of select="$response"/>

    </xsl:message>

    <xsl:variable name="encdata" select="dp:binary-encode($Filename2)"/>

    <xsl:message dp:priority="error">encdata: <xsl:value-of select="$encdata"/>

    </xsl:message>

    </xsl:template>

    </xsl:stylesheet>


    • This xslt below parse the input context into binary node and send it to the backend in base64 format


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

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dp="http://www.datapower.com/extensions" xmlns:regexp="http://exslt.org/regular-expressions" extension-element-prefixes="dp regexp" exclude-result-prefixes="dp regexp" version="1.0">

    <dp:input-mapping href="store:///pkcs7-convert-input.ffd" type="ffd"/>

    <xsl:template match="/">

    <xsl:variable name="FrontsideFTP" select="dp:variable('var://service/URL-in')"/>

    <xsl:variable name="Filename" select="regexp:match($FrontsideFTP, '([^\/]*)$')"/>

    <xsl:variable name="Filename2" select="substring-before($Filename,'?Rename')"/>

    <xsl:variable name="BacksideFTP" select="concat('sftp://admin:Passxxxx/home/admin/File/FTP/',$Filename2)"/>

    <dp:set-variable name="'var://service/routing-url'" value="$BacksideFTP"/>

    <xsl:message dp:priority="debug">OId URL: <xsl:value-of select="$FrontsideFTP"/>

    </xsl:message>

    <xsl:message dp:priority="debug">Filename: <xsl:value-of select="$Filename"/>

    </xsl:message>

    <xsl:message dp:priority="debug">Filename2: <xsl:value-of select="$Filename2"/>

    </xsl:message>

    <xsl:message dp:priority="debug">New URL: <xsl:value-of select="$BacksideFTP"/>

    </xsl:message>

    <xsl:copy-of select="*"/>

    <xsl:variable name="response">

    <dp:url-open target="{$BacksideFTP}" response="ignore">

    <xsl:value-of select="dp:binary-encode(/object/message/node())"/>

    </dp:url-open>

    </xsl:variable>

    <xsl:message dp:priority="error">responseL: <xsl:value-of select="$response"/>

    </xsl:message>

    <xsl:variable name="encdata" select="dp:binary-encode($Filename2)"/>

    <xsl:message dp:priority="error">encdata: <xsl:value-of select="$encdata"/>

    </xsl:message>

    </xsl:template>

    </xsl:stylesheet> 



    #DataPower
    #Support
    #SupportMigration


  • 12.  RE: Convert .zip file to base64 and send it to backend on Datapower

    Posted Tue December 22, 2020 10:46 AM

    Hi ,


    Thanks its working .

    Also can we done this by using gatewayscript .


    Regards ,

    San



    #DataPower
    #Support
    #SupportMigration


  • 13.  RE: Convert .zip file to base64 and send it to backend on Datapower

    Posted Mon December 28, 2020 08:58 AM

    ​Hello

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

    var urlopen = require('urlopen');

    var options = {

    target: 'yourUrl',

    method: 'POST',

    data: bufs.toString('base64')

      };

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

      if (error) {

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

    session.output.write("urlopen error: "+JSON.stringify(error));

      } else {

    // get the response status code

    var responseStatusCode = response.statusCode;

    var responseReasonPhrase = response.reasonPhrase;

    console.log("Response status code: " + responseStatusCode);

    console.log("Response reason phrase: " + responseReasonPhrase);

    // reading response data

    response.readAsBuffer(function(error, responseData){

      if (error){

    throw error ;

      } else {

    session.output.write(responseData) ;

      }

    });

      }

    });

     

    });




    #DataPower
    #Support
    #SupportMigration