How can I make a log file of Json requests and responses?

Discussions relating to the ScottKlement.com port of the open source YAJL JSON Reader/Generator. This includes the YAJL tool as well as the YAJLR4, YAJLGEN, YAJLINTO and YAJLDTAGEN add-ons from ScottKlement.com. http://www.scottklement.com/yajl/
Post Reply
Dario
Posts: 1
Joined: Wed Jul 28, 2021 6:38 am

How can I make a log file of Json requests and responses?

Post by Dario »

I am using YAJL to read and write Json requests and replies in cgi programs as in following;
How can I make a log file of Json requests and responses?
Thank's a lot
Dario

ctl-opt dftactgrp(*no) actgrp('TEST') decedit('0.')
bnddir('YAJL':'QC2LE')
option(*srcstmt: *nodebugio: *noshowcpy);

/include YAJL/qrpglesrc,YAJL_H

dcl-s request char(500);
dcl-s errmsg varchar(500);

docNode = yajl_stdin_load_tree(*on: errMsg);

if errMsg <> '';
errMsg = 'json parse: ' + errMsg;
yajl_writeStdout(500: errMsg);
return;
endif;

node = yajl_object_find(docNode: 'request');
if node = *null;
request= 'mandatory';
else;
request= yajl_get_string(node);
endif;

yajl_tree_free(docNode);
yajl_genOpen(*off);
yajl_beginObj();
yajl_addChar('Request is ': request );
yajl_endObj();
yajl_writeStdout(200: errMsg);
yajl_genClose();
return;
Scott Klement
Site Admin
Posts: 666
Joined: Sun Jul 04, 2021 5:12 am

Re: How can I make a log file of Json requests and responses?

Post by Scott Klement »

If you are on a recent version of YAJL, the yajl_stdin_load_tree subprocedure has a 3rd optional parameter that is an IFS path name that it can save the request to. If that file isn't sufficient and it needs to be in a separate log, you will need to write the data to a temporary file, then read it into your own program and write it to the log from there.

For the response, before you call yajl_writeStdout(), call yajl_saveBuf() to save it to a file or yajl_copyBufStr() to copy it to a string variable and write it to a file yourself.
User avatar
Ticonderogah
Posts: 3
Joined: Wed Jul 28, 2021 12:05 pm

Re: How can I make a log file of Json requests and responses?

Post by Ticonderogah »

Scott's first suggestion is the one I like. I save all my JSON request/response payloads in the IFS.
Post Reply