× 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 Scott,

Thank you for the response.

I want to return the PDF inside the JSON, so I'll change the content type to "application/json".

I need the JSON for some other response messages.

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).

I'm busy doing the code for base64 encoding as suggested by Brad.

I'm sure that will sort the problem.

Best Regards,

Toni Schaake.






-----Original Message-----
From: WEB400 <web400-bounces@xxxxxxxxxxxx> On Behalf Of Scott Klement
Sent: 31 October 2018 07:52
To: web400@xxxxxxxxxxxx
Subject: Re: [WEB400] Return PDF in Rest Service

Hi Toni,

The content type "application/pdf" doesn't make sense, as you're not returning a PDF.  (Not directly, that is.)  You're returning a JSON document.  One of several fields in the JSON document is meant to contain the PDF (that's what you told us) but it is still a JSON document, and therefore the proper content type would be "application/json"

The type of "application/pdf" would be used if you were sending the PDF directly without putting it inside a JSON document.

This, as well as the mention of content-disposition, makes me wonder if perhaps you didn't mean to put the PDF inside JSON? Maybe you really wanted to return the PDF directly?  That would certainly be a lot more commonplace of a task.

You mention that the PDF looks like "garbage", but it seems like that would be expected in this case?  After all a PDF document is a binary document, it is not made up of human-readable text (at least, not always).  So you should expect binary data.  Typically when putting binary data inside a JSON (or similar) document, you'd want to base64 encode it (like Brad said) but, that's only going to be useful if the receiving side expects it in that format.

Personally, that's where I would start...  get a clear picture of what you're expected to send.  Reading this thread, I'm not at all sure.

--
Scott Klement
http://www.scottklement.com


On 10/30/18 8:54 AM, Toni Schaake wrote:
Hi Bradley,

Thank you for your prompt response.

The CCSID of the PDF is 819 on the IFS.

I'm building a JSON Object to return to the client. It looks like this:

[
{
"UploadStatus": "A",
"UploadMessage": "Biometric Info Captured",
"HHSCore": "RED"
},
{
"ResponseDocument": "%PDF-1.7\n%äãà Ò\n4......... ==> this data is corrupted.
}
]

I was hoping to load the PDF with yajl_addCharStmf('ResponseDocument' : DocPath: ErrMsg). Docpath is where the PDF Doc is stored on the IFS.

yajl_writeStdout(sts: msg); does not allow for content-type of application/pdf, and I need Content-Disposition in the header as well, hence the need to do QtmhWrStout.

Best Regards,

Toni


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

What Code page is the PDF in your IFS?

What format is the PDF returned to client 2? It looks like a straight PDF
file... so why are there yajl functions in it and building arrays, etc?

Where are you loading the PDF in this code? Is it already in jsonBuf? How did it get there? From other code not in the sample source?

If you were to use something like eRPG SDK or CGIDEV2 to return the PDF document it would be a lot easier. Write out headers, load the PDF as the template, and write it out. eRPG example:

#startup();
#writeTemplate('stdPDFheader.erpg'); <--- this contains
headers like Content-type: application/pdf with 2 new lines
#setCCSIDStdOut(1208);
#writeTemplate(yourPDFFile);
#cleanup();

*INLR = *on;



Bradley V. Stone
www.bvstools.com
MAILTOOL Benefit #6 <https://www.bvstools.com/mailtool.html>: Easily send group emails with Distribution Lists

On Tue, Oct 30, 2018 at 8:01 AM Toni Schaake <Toni.Schaake@xxxxxxxxxxxxxx>
wrote:

Hi there,
I'm having great difficulty in returning a PDF Document in a REST
Service request.

Background:

I'm facilitating a Webservice between 2 External parties

I receive a JSON request from Client 1, parse the info, call an API
(Via Scott's HTTPAPI) on Client 2's Systems, receive the response as a
PDF Document which I store on the IFS.

Now I need to pass this PDF Document back to Client 1 as a response to
their API call, and this is where I'm getting Garbage in the PDF Document.

Code Snippet:

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();
yajl_addCharStmf('ResponseDocument' : DocPath: ErrMsg);
yajl_endObj();
yajl_endArray();
sts = 200;
// This is the response I log for error\troubleshoot\later
investigations etc.
Response = '[{"UploadStatus":"A",' +
'"UploadMessage:"' +%Trim(RspString) + '",' +
'"HHSCore":"' + %Trim(w_HHSCore) + '"},{'+
'"ResponseDocument":"' + %Trim(Docpath) + '}]';
doLogging();
// the following code acts like yajl_writeStdout. I need
content-type to be different
rc = yajl_getBuf( jsonBuf: jsonSize );
Headers = 'Status: ' + %editc(sts:'X') + CRLF +
'Content-type: application/pdf' + CRLF +
// 'Content-type: application/json charset=utf-8' + CRLF +
// 'Content-type: application/octet-stream' + CRLF +
// 'Transfer-Encoding: identity' + CRLF +
'Content-Encoding : gzip' + CRLF +
'Content-Disposition: attachment; filename=' +
%Trim(w_File) + CRLF + CRLF;
QtmhWrStout ( %addr(Headers) + 2 : %len(Headers) : APIError );
offset = 0;
dow jsonSize > 0;
length = jsonSize;
if length > ChunkSize;
length = ChunkSize;
endif;
QtmhWrStout ( jsonBuf + offset : length : APIError );
jsonSize -= length;
offset += length;
enddo;
//yajl_writeStdout(sts: msg); yajl_genClose();

This runs fine and returns the Document, but the PDF is all garbage.
I'm not too sure if yajl_addCharStmf() is the correct procedure to use.

I'm using both Postman and SOAPUI for testing.

Kind Regards,

Toni Schaake.




--
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.


--
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.