There are several ways to encode and decode Base64 messages in SAP PI/PO and CPI. 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.
We will look at how to encode and decode Base64 string in SAP Integration Suite on BTP (CPI). First, let’s look at the standard options provided by SAP Integration Suite to transform Base64 and then we will develop a couple of UDFs using Groovy Script.
SAP Versions used in the illustration:
- SAP PO 7.5
- SAP Integration Suite on BTP (CPI)
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 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:
<?xml version="1.0" encoding="UTF-8"?> <ns0:Base64InputMessage xmlns:ns0="urn:Source_System:Base64InputMessage"> <EncodedString>U0FQIEludGVncmF0aW9uIEh1Yg==</EncodedString> <PlainString>Lets learn something new!</PlainString> </ns0:Base64InputMessage>
Receiver Message Type and Sample:
<?xml version="1.0" encoding="UTF-8"?> <ns1:Base64OutputMessage xmlns:ns1="urn:Target_System:Base64OutputMessage"> <DecodedString>SAP Integration Hub</DecodedString> <EncodedString>TGV0cyBsZWFybiBzb21ldGhpbmcgbmV3IQ==</EncodedString> </ns1:Base64OutputMessage>
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.
Function Library View:
Code Snippet:
//Variable to assign base64 encoded string
String encodedString = null;
try{
//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:
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.
Functions Library View:
Code Snippet:
//Variable to hold decoded string
String decodedString = null;
try{
//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;
Mapping:
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=”.
Encode and Decode Base64 in SAP Integration Suite on BTP (CPI) Using Standard Functions
Now let’s look at how to decode Base64 in SAP Integration Suite CI (CPI). There are several ways to handle Base64 format in SAP Integration Suite. You can use the standard functionality provided by SAP, or create a custom Groovy Script.
Let’s look at the standard options provided out of the box in SAP Integration Suite, then we will move on to creating two UDFs using Groovy Script.
Decode Base64 Using Standard Functionality
I will create an iFlow to decode the Base64-encoded string in the incoming XML message.
Configure a Content Modifier to Extract the Base64 String
Let’s extract the embedded Base64 String in the XML element to an exchange property.
Set the Base64 encoded string in the body of the message using a simple camel expression.
Because the standard Base64 decoder in CI simply decodes the payload or the body of the message, we will set the payload with the Base64 encoded string before we pass the message to the decoder.
Add a Base64 Decoder
Add a Base64 Decoder from the menu option “Transformation” > “Decoder”.
Test the Base64 Decoder iFlow Using Postman
The service will return the decoded string in the response body.
Similarly, you can also build an interface to Encode the body of the message to Base64 using the standard Base64 Encoders provided by SAP Integration Suite CI (CPI).
How to Convert Base64 Using User-Defined-Functions (UDF) in SAP Integration Suite (CPI)
Instead of the standard encoder and decoder steps, let’s use a custom message mapping step to map the incoming XML message to the target XML format.
Create New Message Mapping
Create Two New Custome UDFs
Now create two custom UDFs, one to encode and the other to decode Base64.
Groovy Script UDF to Decode Base64 in SAP Integration Suite
Here is a sample UDF that can be used in SAP Integration Suite to decode a Base64 encoded string to plain text.
Decode Base64 – UDF Code Snippet
import com.sap.it.api.mapping.*;
import java.util.Base64;
def String decodeBase64(String encodedString){
// Check if the input string is null or empty
if (encodedString == null || encodedString.isEmpty()) {
return "";
}
// Decode the Base64 string
try {
byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
return new String(decodedBytes, "UTF-8");
} catch (Exception e) {
// Handle any decoding exception
return "Error decoding Base64: " + e.getMessage();
}
}
Map the encoded string to target the decoded string field by using the custom UDF.
Groovy Script UDF to Encode Base64 in SAP Integration Suite
Encode Base64 – UDF Code Snippet
import com.sap.it.api.mapping.*;
import java.util.Base64;
def String encodeToBase64(String plainString){
// Check if the input string is null or empty
if (plainString == null || plainString.isEmpty()) {
return "";
}
// Encode the string to Base64
try {
byte[] encodedBytes = Base64.getEncoder().encode(plainString.getBytes("UTF-8"));
return new String(encodedBytes, "UTF-8");
} catch (Exception e) {
// Handle any encoding exception
return "Error encoding to Base64: " + e.getMessage();
}
}
Test the UDFs
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?
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,
Bhargav.
Hello Bhargav,
Thank you! Glad to hear the article was helpful.
Please send me an email to [email protected]
Cheers!
Isuru
Hi,
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.
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
Thanks