Scott and Jon - thanks for your replies.
I guess I assumed the data needed to be converted to UTF-8 before encoding. I may try it w/out doing the conversion.
The max size of the entire SOAP message is 4Gb (header, envelope and body), so I know my variable will need to be larger. I may actually just write the XML out to the IFS, then read it back in (so I can check the size).
-----Original Message-----
From: RPG400-L [mailto:rpg400-l-bounces@xxxxxxxxxxxxxxxxxx] On Behalf Of Scott Klement
Sent: Friday, May 17, 2019 2:30 AM
To: rpg400-l@xxxxxxxxxxxxxxxxxx
Subject: Re: Base64 Encoding
Greg,
So you have EBCDIC XML data, and you want to convert it to UTF-8 and then base64 encode it? That's certainly doable, but I've never seen a web service that wants it done that way. But, assuming that's what you need, then your code would look as follows. I will add comments to explain what each line does:
// tell httpapi to translate from job ccsid (0) to UTF-8 (1208) or vice-versa
http_setCCSIDs( 1208: 0 );
// Translate xmlData to UTF-8 using a pointer to dynamic memory
utflen = HTTP_xlatedyn( %len(xmlData)
: %addr(xmlData:*DATA)
: TO_ASCII
: p_xmlDataUTF );
// Set output field to max length -- this is important to avoid RPG // erasing the data when the length is set later
%len(xmlData64) = %len(xmlData64:*MAX);
// Convert UTF-8 string to base64
b64len = base64_encode( p_xmlDataUTF
: utflen
: %addr(xmlData64:*DATA)
: %len(xmlData64:*MAX) );
// Set length to only what was needed for the base64 data
%len(xmlData64) = b64len;
// Free up the dynamic string, we're done with it
dealloc p_xmlDataUTF;
You might consider making xmlData64 larger (utf-8 can make strings longer, as some special characters can take up multiple bytes. then, base64 encoding it will also make it 33% longer.) So if your input data can be up to 2048 characters (which is very small for an xml doc) then the max size of the output of this routine would be 10924 bytes. It's unlikely that it'd be that large, but there's no reason not to account for the worst case scenario. (Especially since you're only talking about appeox 10k, a tiny piece of memory by today's standards.)
Good luck!
On 5/16/19 2:36 PM, Greg Wilburn wrote:
Is this the easiest way to Base64 encode and XML document (either in an RPG variable or on the IFS) for use in a SOAP web service?
I've used Scott's BASE64R4 service program for smaller things like API keys, etc.
I've been trying to mock-up a test scenario, and I'm not getting the results I expected - pretty sure I'm doing something wrong with the varchar fields. The code I'm copying was using much smaller fixed length fields.
xmlData and xmlData64 are both varchar(2048)... however the data is nowhere near that large (utflen=710).
http_setCCSIDs( 1208: 0 );
utflen = HTTP_xlatedyn( %len(%trim(xmlData))
: %addr(xmlData)
: TO_ASCII
: p_xmlDataUTF );
b64len = base64_encode( p_xmlDataUTF
: utflen
: %addr(xmlData64)
: %size(xmlData64) );
dealloc p_xmlDataUTF;
My resulting data in xmlData64 does not match the online encoders.
Thanks,
Greg
--
Scott Klement
http://www.scottklement.com
--
This is the RPG programming on IBM i (RPG400-L) mailing list To post a message email: RPG400-L@xxxxxxxxxxxxxxxxxx To subscribe, unsubscribe, or change list options,
visit:
https://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives at
https://archive.midrange.com/rpg400-l.
Please contact support@xxxxxxxxxxxx for any subscription related questions.
Help support midrange.com by shopping at amazon.com with our affiliate link:
https://amazon.midrange.com
As an Amazon Associate we earn from qualifying purchases.