× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Hi All,

Success!

A big thank you to everybody who responded.

I'm posting the solution here for anybody that might need it in future. This is based on Scott's YAJL-tool.

In Sub-proc Dofilestuff I Read the PDF into a buffer. Note the CCSID 819 when opening the file. I then create a new buffer (base64data) and translate the buffer to base64 into the newly created buffer. Then I add it to the existing json buffer.(yajl_addCharPtr()).
Note the size calculation when creating the base64data buffer. (Size of base64 differs, so I calculate the amount of space to allocate).

After that it's as simple as writing the headers and data to stdout.

This might not be the most elegant\cleanest way of going about it, but for now I'm happy. Testing can resume and a few cold beverages are in order methinks.

yajl_genOpen(*on);
yajl_beginArray();
yajl_beginObj();
yajl_addChar('UploadStatus' : 'A');
yajl_addChar('UploadMessage' : %Trim(RspString));
yajl_addChar('HHSCore' : %Trim(w_HHSCore));
yajl_endObj();
yajl_beginObj();
// read file on IFS into buffer and encode.
DoFileStuff();
yajl_endObj();
yajl_endArray();
rc = Yajl_getBuf(jsonbuf:jsonSize);
yajl_GenClose();
sts = 200;
Headers = 'Status: ' + %Char(sts) + CRLF +
'Content-type: application/json charset=utf-8' + CRLF +
'Content-Disposition: attachment; filename=' + %Trim(w_File) + CRLF + CRLF;
WriteToApache( %addr(Headers) + 2 : %len(Headers) : APIError );
WriteToApache( jSonBuf : jsonSize : APIError );

Dcl-proc DoFileStuff;
FStat(%trim(DocPath):%Addr(ds_Stat));
statptr = %Addr(ds_Stat);
Flags = O_RDONLY + O_TEXTDATA + O_CCSID;
FileId = openF( %trim(DocPath):flags:0:819);
filedata = yajl_exbuf_new( 32768: 819 );
Size = readF( FileId: %addr(buftxt): 65534);
Dow Size > 0;
yajl_exbuf_concat_ptr( filedata: %addr(buftxt): Size );
Size = readF( FileId: %addr(buftxt): 65534);
Enddo;
callp closeF(FileId);
base64data = yajl_exbuf_new( 4*(Filedata.len/3) + 3 : 819 );
rp = base64_encode(Filedata.ptr:Filedata.len:base64data.ptr:base64data.size);
base64data.len = rp;
yajl_addCharPtr('ResponseDocument' : base64data.ptr: base64data.len);
yajl_exbuf_free(filedata);
yajl_exbuf_free(base64data);
End-proc DoFileStuff;

-----Original Message-----
From: WEB400 <web400-bounces@xxxxxxxxxxxx> On Behalf Of Tim Fathers
Sent: 05 November 2018 15:38
To: Web Enabling the IBM i (AS/400 and iSeries) <web400@xxxxxxxxxxxx>
Subject: Re: [WEB400] Return PDF in Rest Service


By the way, if you don't want to use YAJL, another(easier!) option is to use SQL, which can naively build JSON objects and might help you overcome the 12M limit, below is a small example.

dcl-proc main2;
dcl-pi *N end-pi;

dcl-s jsonDoc sqltype(CLOB: 32767);
dcl-s base64doc sqltype(CLOB: 32767);

dcl-s status varchar(10) inz('A');
dcl-s message varchar(256) inz('Biometric Info Captured');
dcl-s hhscore varchar(10) inz('RED');


exec sql set :base64doc = 'dsdslkflkfdslkfgdgdg';

exec sql
set :jsonDoc = json_object(
'UploadStatus': :status,
'UploadMessage': :message,
'HHSCore': :hhscore,
'document': :base64doc
);

end-proc;

Also, the format of your output JSON object looked odd to me:
[
{
"UploadStatus": "A",
"UploadMessage": "Biometric Info Captured",
"HHSCore": "RED"
},
{
"ResponseDocument": "%PDF-1.7\n%äãà Ò\n4......... ==> this data is corrupted.
}
]

..is an array of two different shaped objects, I would expect it to look more like:

{
"UploadStatus": "A",
"UploadMessage": "Biometric Info Captured",
"HHSCore": "RED",
"ResponseDocument": {
"type": "application/pdf",
"data": "ASbdhbesjwwmsmewejwewee=="
}
}
________________________________
From: WEB400 <web400-bounces@xxxxxxxxxxxx> on behalf of Toni Schaake <Toni.Schaake@xxxxxxxxxxxxxx>
Sent: 05 November 2018 13:52
To: Web Enabling the IBM i (AS/400 and iSeries)
Subject: Re: [WEB400] Return PDF in Rest Service

Hi Tim,

Thank you for the reply.

I did use the online encoder as mentioned in your previous reply. The data is the same after encoding.

Now I need to get that data into the JSON String.

I'm thinking of building the reply string without using YAJL, and writing it to the webserver with StdOut.

Best Regards,

Toni.

-----Original Message-----
From: WEB400 <web400-bounces@xxxxxxxxxxxx> On Behalf Of Tim Fathers
Sent: 05 November 2018 14:04
To: Web Enabling the IBM i (AS/400 and iSeries) <web400@xxxxxxxxxxxx>
Subject: Re: [WEB400] Return PDF in Rest Service

...this is about the quickest and dirtiest code I can come up with that will convert your PDF into base-64, using embedded SQL. Be careful, there's no error checking or validation of any kind and I wouldn't use SQL to do the base64 encoding in production in this case because it's quite slow and can only encode chunk of 2732 bytes, the apr_base64_* APIs are almost certainly a better choice. I'm not familiar with the YAJL library, but as far as I can see, you have to pass a character string to it in this case and so you'd be limited to PDFs of about 3/4s of the size of the maximum RPG character field length (about 12MB).

**FREE
ctl-opt main(main);

dcl-proc main;
dcl-pi *N end-pi;

dcl-c CHUNK_SIZE 2732;
dcl-s fileName varchar(630);
dcl-s fileBlob sqltype(BLOB_LOCATOR);
dcl-s start int(10) inz(1);
dcl-s bytesLeft int(10);
dcl-s base64ChunkLen int(5);
dcl-s base64Chunk varchar(4096);
dcl-s base64doc varchar(16000000);

filename = '/document.pdf';

exec sql set :fileBlob = get_blob_from_file(:fileName);
exec sql set :bytesLeft = length(:fileBlob);

dow bytesLeft > 0;
base64ChunkLen = %min(CHUNK_SIZE: bytesLeft);
exec sql
set :base64Chunk = systools.base64encode(
substr(:fileBlob, :start, :base64ChunkLen)
);

base64doc += base64Chunk;
bytesLeft -= base64ChunkLen;
start += base64ChunkLen;
enddo;

// yajl_...
// yajl_addChar('document': base64doc);
// yajl_...

end-proc;


________________________________
From: WEB400 <web400-bounces@xxxxxxxxxxxx> on behalf of Toni Schaake <Toni.Schaake@xxxxxxxxxxxxxx>
Sent: 05 November 2018 10:53
To: Web Enabling the IBM i (AS/400 and iSeries)
Subject: Re: [WEB400] Return PDF in Rest Service

Hi All,

Thank you all for the advice.

I've made the changes to encode the PDF to Base64, but still no luck.

Does anybody have some sample code, as I'm really stuck here and need to get it to work ASAP.

I'm using Scott's YAJL tools, and HTTPAPI handle the request to the server where the PDF originates from. (Using Http_Req(), also tried http_Stmf()).

Thank you in advance,

Toni Schaake.

-----Original Message-----
From: WEB400 <web400-bounces@xxxxxxxxxxxx> On Behalf Of Charles Wilt
Sent: 31 October 2018 17:28
To: Web Enabling the AS400 / iSeries <web400@xxxxxxxxxxxx>
Subject: Re: [WEB400] Return PDF in Rest Service

On Wed, Oct 31, 2018 at 1:29 AM Toni Schaake <Toni.Schaake@xxxxxxxxxxxxxx>
wrote:

I understand that the PDF, when viewed as raw data is already
"garbage", but in my test the "garbage" does not look the same as the
original saved PDF. (Hope you understand what I mean here).



Probably your code (and/or the i) didn't treat the document as binary, thus it was translated from to ASCII

Make you write/read from the IFS as binary, not text.

Charles
--
This is the Web Enabling the IBM i (AS/400 and iSeries) (WEB400) mailing list To post a message email: WEB400@xxxxxxxxxxxx To subscribe, unsubscribe, or change list options,
visit: https://eur04.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.midrange.com%2Fmailman%2Flistinfo%2Fweb400&amp;data=02%7C01%7C%7C4ee35fe37de74a285d4308d6431d9238%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636770191610576441&amp;sdata=FSR%2FhceiIGAWhskEDX1kpyUAuMDqABEvwP99W%2F3beKo%3D&amp;reserved=0
or email: WEB400-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives at https://eur04.safelinks.protection.outlook.com/?url=https%3A%2F%2Farchive.midrange.com%2Fweb400&amp;data=02%7C01%7C%7C4ee35fe37de74a285d4308d6431d9238%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636770191610576441&amp;sdata=GRCnI9t3kkgyeD62eWf9Cb626jYZPGU6dHk8DM8uuVQ%3D&amp;reserved=0.

--
This is the Web Enabling the IBM i (AS/400 and iSeries) (WEB400) mailing list To post a message email: WEB400@xxxxxxxxxxxx To subscribe, unsubscribe, or change list options,
visit: https://eur04.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.midrange.com%2Fmailman%2Flistinfo%2Fweb400&amp;data=02%7C01%7C%7C4ee35fe37de74a285d4308d6431d9238%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636770191610576441&amp;sdata=FSR%2FhceiIGAWhskEDX1kpyUAuMDqABEvwP99W%2F3beKo%3D&amp;reserved=0
or email: WEB400-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives at https://eur04.safelinks.protection.outlook.com/?url=https%3A%2F%2Farchive.midrange.com%2Fweb400&amp;data=02%7C01%7C%7C4ee35fe37de74a285d4308d6431d9238%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636770191610576441&amp;sdata=GRCnI9t3kkgyeD62eWf9Cb626jYZPGU6dHk8DM8uuVQ%3D&amp;reserved=0.

--
This is the Web Enabling the IBM i (AS/400 and iSeries) (WEB400) mailing list To post a message email: WEB400@xxxxxxxxxxxx To subscribe, unsubscribe, or change list options,
visit: https://eur04.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.midrange.com%2Fmailman%2Flistinfo%2Fweb400&amp;data=02%7C01%7C%7C4ee35fe37de74a285d4308d6431d9238%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636770191610576441&amp;sdata=FSR%2FhceiIGAWhskEDX1kpyUAuMDqABEvwP99W%2F3beKo%3D&amp;reserved=0
or email: WEB400-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives at https://eur04.safelinks.protection.outlook.com/?url=https%3A%2F%2Farchive.midrange.com%2Fweb400&amp;data=02%7C01%7C%7C4ee35fe37de74a285d4308d6431d9238%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636770191610576441&amp;sdata=GRCnI9t3kkgyeD62eWf9Cb626jYZPGU6dHk8DM8uuVQ%3D&amp;reserved=0.

--
This is the Web Enabling the IBM i (AS/400 and iSeries) (WEB400) mailing list To post a message email: WEB400@xxxxxxxxxxxx To subscribe, unsubscribe, or change list options,
visit: https://eur04.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.midrange.com%2Fmailman%2Flistinfo%2Fweb400&amp;data=02%7C01%7C%7C4ee35fe37de74a285d4308d6431d9238%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636770191610576441&amp;sdata=FSR%2FhceiIGAWhskEDX1kpyUAuMDqABEvwP99W%2F3beKo%3D&amp;reserved=0
or email: WEB400-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives at https://eur04.safelinks.protection.outlook.com/?url=https%3A%2F%2Farchive.midrange.com%2Fweb400&amp;data=02%7C01%7C%7C4ee35fe37de74a285d4308d6431d9238%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636770191610576441&amp;sdata=GRCnI9t3kkgyeD62eWf9Cb626jYZPGU6dHk8DM8uuVQ%3D&amp;reserved=0.

--
This is the Web Enabling the IBM i (AS/400 and iSeries) (WEB400) mailing list To post a message email: WEB400@xxxxxxxxxxxx To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/web400
or email: WEB400-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives at https://archive.midrange.com/web400.


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

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.