Base64 Function Modules in SAP ABAP
The base64 algorithm is an efficient way to transfer data via the Internet. In SAP, you can use Base64 when you are implementing file-based interfaces between your SAP system and third-party applications via SAP Process Orchestration or SAP Cloud Platform Integration. Instead of sending the file as an attachment in a message you can send file data as a Base64 string. For example, for outbound interfaces, you first need to encode the file in Base64 format and transmit it to the target system as a Base64 string. In the target system, you need to decode the Base64 string back to actual data.
This method comes in handy if you want to implement file-based interfaces via the Application Integration Framework (AIF) as AIF is not able to handle file attachments.
This article goes into detail on Function Modules (FM) in SAP that we can use to encode and decode Base64 string. Use encoding FMs for outbound interface messages and decoding FMs for inbound interface messages.
Function Modules for Base64 Encoding:
There are two function modules to encode a string to Base64 format. First, convert string to Xstring using ‘SCMS_STRING_TO_XSTRING‘. Then use ‘SCMS_BASE64_ENCODE_STR‘ to convert Xstring to Base64.
Function Modules for Base64 Decoding:
Decoding is a three-step procedure. Convert Base64 string to Xstring using ‘SCMS_BASE64_DECODE_STR‘. Next, transform the Xstring to Binary using function module ‘SCMS_XSTRING_TO_BINARY‘. Finally, turn the Binary format to String via ‘SCMS_BINARY_TO_STRING‘.
How to Decode and Encode Base64 in SAP PI/PO and CPI
In most instances, you will use SAP Process Orchestration (PO) to interface between SAP and third-party applications. In these instances, we can use mapping techniques such as User Defined Functions (UDFs), Java Mappings or Adapter Modules to decode and encode Base64.
However, CPI of SAP Integration Suite has standard converter modules to encode and decode Base64.
I have also developed a sample program to decode/encode base64 in ABAP.
To summarize, you can use Base64 string to transfer data such as files, PDFs, images, etc. between SAP and external APIs. Using this method you can avoid sending files as attachments in the message payload. Use the FMs listed above to Encode and Decode Base64 in your SAP application or interface program.
Thank you for reading, hope this information will help you design interfaces.
i would like to add that there are also BASE64 methods in CL_HTTP_UTILITY class:
And I personally prefer to use this class instead of (unreleased) functions.
Certainly, CL_HTTP_UTILITY class methods you have given can be used to encode and decode base64. I have included these methods in my original post.
Thank you for this blog!
I'm getting a corrupted PDF with this code:
Any clue about what can be wrong?
Your code looks completely correct. Are you sure the PDF base64 is correct at the beginning?
Yes, I'm sure! If I perform this I get my base64 file.
Using an online base64 decoder like https://www.freeformatter.com/base64-encoder.html I'm able to get a working PDF...
I don't know if it can be an issue of my SAP server... Something about encoding... I'm on a ECC6 EHP8 SP8 (BASIS 750 SP9).
I guess I have a good hint. If I perform this:
My base64 PDF gets corrupted.
The good one is the one that has ">>". I get more differences with this "ÿ" strange character in the files...
I've tried all the base64 decode function modules and class methods that I could find in my system and I always get the same result...
As I said, no issue at my side with your code, the base64 decoding works and I can open the downloaded file correctly:
Thank you for your time.
When I validate the base64 here I get the following message:
"This encoding [base64url] is technically identical to the previous one [base64], except for the 62:nd and 63:rd alphabet character, as indicated in Table 2 [- instead of + and _ instead of /]." (https://tools.ietf.org/html/rfc4648#section-5)
You are totally right! It works fine now!
Thank you so much for your time!
I think there is an issue with CL_HTTP_UTILITY=>DECODE_X_BASE64 when new lines/linebreaks are present.
According to this post https://superuser.com/questions/1225134/why-does-the-base64-of-a-string-contain-n we can have new-lines in a base64 encoded string. An old IETF RFC specified a new line after every 76 characters.
From what I'm seeing the CL_HTTP_UTILITY method does not expect new lines and we get a garbled result. The SCMS FM manages to cope with new lines.
If you are using CL_HTTP_UTILITY I suggest removing new lines from your base64 string prior to decoding (perhaps removing chars in CL_ABAP_CHAR_UTILITIES=>CR_LF)
"secure and efficient", can you elaborate on this part? From a security perspective, the data is not encrypted or checksummed, for efficiency, base64 encoded data is typically larger than the original.
If you want to add security, transmit the data using HTTPS or SNC 😉
If you talk about SCMS_STRING_TO_XSTRING then you may complete that it encodes the string in UTF-8 by default. Idem for the decoding, you convert from a xstring text in UTF-8. Note that instead of all those old function modules, you may use CL_ABAP_CODEPAGE (ABAP 7.02 to 7.52), that's much shorter:
Thank you for sharing Sandra!
Thank you for sharing Sandra!
Nice work Fernando.
I used it to check my data sent using CPI.
Thanks a lot.