Base64 Encoding and Decoding UDFs for PI/PO (Example)

There are several ways to encode and decode Base64 messages in SAP PI/PO. In this post, we will discuss how to convert Base64 using User Defined Functions (UDF).

You can also use Java Mapping class to encode or decode Base64 format. In SAP Back-end system the same functionality can be achieved using ABAP Function Modules or ABAP class CL_HTTP_UTILITY.

SAP Versions used in the illustration:

  • SAP PO 7.5

Encoding and Decoding Example Overview:

Let’s assume, we have a source XML message with two fields. One field contains the Base64 encoded string and the other field contains the string we need to encode to Base64.

base64 encoding and decoding using UDFs example. Example Overview: Functionality of the UDFs and expected result. Graphical mapping tool test.


Example Overview: Functionality of the UDFs and expected result.

Base64 encoded message is included in source message element <EncodedString>. UDF decodes this message. Decoded message will be mapped to target field <DecodedString>.

The text we should convert to Base64 is included in the source message node <PlainString>. The value will be encoded to Base64 format using an UDF and mapped to target XML node <EncodedString>.

Sender Message Type and Sample:

Source Message Type for base64 conversion example.


Source Message Type
<?xml version="1.0" encoding="UTF-8"?>
<ns0:Base64InputMessage xmlns:ns0="urn:Source_System:Base64InputMessage">
   <PlainString>Lets learn something new!</PlainString>

Receiver Message Type and Sample:

Target Message Type for base64 encoding and decoding UDF example in ESR


Target Message Type
<?xml version="1.0" encoding="UTF-8"?>
<ns1:Base64OutputMessage xmlns:ns1="urn:Target_System:Base64OutputMessage">
   <DecodedString>SAP Integration Hub</DecodedString>

This example will be implemented in an SAP Process Orchestration (PO) system version SAP PO7.50 single stack.

Design of the Base64 Encoding and Decoding UDFs:

We will implement two UDFs, one to encode Base64 and another one to decode Base64.

We will name,

  • Encoding UDF as Base64Encoder and
  • Decoding UDF ad Base64Decoder.

Both UDFs have an Execution Type of “Single Value“. That means the UDF accepts only one argument as input and outputs one value. For example, Base64 decoding UDF accepts Base64 encoded string as the input and returns the encoded string.

Both encoding and decoding will be carried out using standard methods of Java utility class java.util.Base64. This class contains two nested classes: Base64.Decoder decodes byte data and Base64.Encoder implements the encoder for encoding byte data.

Base64 Encoding UDF: Base64Encoder

Encoding UDF has one input variable “PlainString”. Input value is assigned from the source message element “PlainString”. UDF converts this value to Base64 format.

Encoding can be broken down to two steps,

Step 1: Convert String to Byte Array

First, input string value is converted to byte array using String method getBytes().

byte[] bytes = PlainString.getBytes(); 

Step 2: Encode Byte Array to Base64 Format

Next, the static method encodeToString of nested class Base64.Encoder decodes the byte array to actual text.

getEncoder() method returns the Base64.Encoder.

encodedString = Base64.getEncoder().encodeToString(bytes);

Complete Encoding UDF:

Make sure to import the java class java.util.Base64 to UDF definition.

java.util.base64 class imported to UDF in function library ESR
java.util.base64 class imported to UDF

Function Library View:

Encoding UDF in function library
Encoding UDF

Code Snippet:

//Variable to assign base64 encoded string
String encodedString = null;


//Convert input argument (inputString) to bytes
byte[] bytes = PlainString.getBytes(); 

//Convert byte array of the input string to base64
encodedString = Base64.getEncoder().encodeToString(bytes);

}catch(Exception e) {


//Export base64 encoded string
return encodedString;


Mapping encoder from source to target in ESR graphical mapping


Mapping encoder from source to target

Decoding UDF also contains only one input variable. UDF accepts Base64 encoded value using input variable “EncodedString”. Function of the UDF is to decode the encoded string value and return plain text.

Decoding can be broken down to two steps,

Step 1: Transform Encoded String to Encoded Byte Array

Decode method of nested class Base64.Decoder converts Base64-encoded string into encoded bytes array.

Method getDecoder() returns the Base64.Decoder class.

 byte[] decodedBytes = Base64.getDecoder().decode(EncodedString);

Step 2: Decode Encoded Byte Array to Text

Next, encoded byte array is converted to string.

String s = new String(decodedBytes);

Complete Decoding UDF:

Make sure to import the java class java.util.Base64 to UDF definition.

java.util.base64 class imported to function


java.util.base64 class imported

Functions Library View:

Complete decoder UDF in ESR function library
Complete decoder UDF

Code Snippet:

//Variable to hold decoded string
String decodedString = null;


//Convert base64 schema to byte array
 byte[] decodedBytes = Base64.getDecoder().decode(EncodedString);

//Get the string of decoded byte array
String s = new String(decodedBytes);
decodedString = s;

}catch(Exception e) {


//Export decoded string
return decodedString;


Source to target mapping for base64 decoder in ESR graphical mapping


Source to target mapping for base64 decoder

Test the UDFs:

Let’s test the User Defined Functions and mapping using graphical message mapping test tool.

Input encoded value is “RGVjb2RlIHRoaXMgdGV4dCE=”. This translates to “Decode this text!”.

Input value which should be encoded to Base64 schema is “Encode this text!”. Base64 converted value of this is “RW5jb2RlIHRoaXMgdGV4dCE=”.

Testing UDFs in graphical mapping. ESR message mapping test tool.


Testing UDFs in graphical mapping
Base64 decoder UDF display queue with input output in ESR graphical mapping


Base64 decoder UDF display queue
Base64 encoder UDF display queue in graphical mapping in ESR


Base64 encoder UDF display queue

If you have any questions about Base64 encoding and decoding using User Defined Functions, Java Mapping, or ABAP applications, leave a comment below! Have you used any other methods to convert Base64 schema in SAP integration scenarios?

4 thoughts on “Base64 Encoding and Decoding UDFs for PI/PO (Example)

  1. bhargav kolla says:

    Hi Fernando,
    It is really helpful.
    Can you help us to learn NW BPM’s and in configuring TPM in B2B Scenarios.
    I would love to learn more from you at any cost.
    If possible u can give a miss call to +9381385619(what’s app).

    Thanks & Regards,

  2. NARESH says:

    We are using RFC function module to get the response from SAP, When we are importing RFC its converted to base 64 format data type but it declared XSTRING at sap side.
    RFC module getting the data from table with binary format but in PO we are getting as base 64 format due to data type is xsd:base64 is response field.
    The end system getting error when they are opening excel/pdf files (error: file may be corrupted open repair mode)
    Could you please help us how we can fix this issue from PO end.

  3. Ashok says:

    hi Fernando

    i have a requirement that i need to decode the SHA 256 algorithm field in PO 7.5
    could you please suggest and help me how to execute this


Leave a Reply

Your email address will not be published. Required fields are marked *