|
As a follow up to my earlier post...
I have this code working (the UTF-8 translation was not necessary). But I'm anticipating a possible issue once the size of the XML payload gets larger. I'm basically keeping everything in memory, and posting to the web service.
The problem occurs when my XML payload gets very large... I would like to write the XML Payload to a file, base64 encode that, then post the SOAP message. Given the current process below, how can I combine the base64 encoded data with the SOAP Header/Envelope, filename and passphrase? I would like to use HTTP_URL_POST_STMF.
One other point is that I will likely have to hit this same web service with other data.
Currently working process:
1. Use CGIDEV2 to create the XML payload (using a template for the structure)
2. Instead of using WrtHtmlToStmf, I use GetHtmlBufferP to get the pointer to my XML data
3. Base64 encode the XML payload buffer (xmlData64)
4. Post the SOAP message (including the filename and passphrase) using HTTP_URL_POST
SOAP=
'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"'+
' xmlns:tem="http://tempuri.org/">'+
'<soapenv:Header/>'+
'<soapenv:Body>'+
'<tem:UploadFile>'+
'<tem:f>' + xmlData64 + '</tem:f>'+
'<tem:fileName>' + filename + '</tem:fileName>'+
'<tem:passphrase>' + passphrase + '</tem:passphrase>'+
'</tem:UploadFile>'+
'</soapenv:Body>'+
'</soapenv:Envelope>';
rc = http_url_post( URL
: %addr(SOAP: *data)
: %len(%trimr(SOAP))
: ResponseStmf
: HTTP_TIMEOUT
: HTTP_USERAGENT
: HTTP_CONTTYPE);
if rc <> 1;
httperror = http_error();
endif;
-----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.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.