Thanks for your response Scott.
Mentioned only outbound, because Inbound code already was working for a different project.
From the payload received from inbound, trying to create same new payload to send to outbound.
Have not tested the outbound yet and planning to do it today.
Would like to know if this is the right approach, or something better or simpler can be done for this scenario.
Was also exploring below SQL command to send the payload outbound:
Exec SQL values QSYS2.HTTP_POST(:url, :request, :options) into :response;
Please advise and let me know if you need further information.
Here is the inbound process:
Process module:
Code: Select all
//Receive input payload
If GetInput( Method: ErrMsg ) = False;
// some code to check for any error message
LeaveSr;
EndIf;
If Method <> 'POST';
// some code to check for any error message
LeaveSr;
Else;
If GetJSONDocument( OrderDS : DocNode);
// some code to send response and handle error
ExSr ResponseRoutine;
yajl_tree_free(DocNode);
Else;
// some code to check for any error message
EndIf;
EndIf;
BegSr ResponseRoutine;
yajl_genOpen(*On);
yajl_beginObj();
yajl_beginObj('order');
yajl_addNum('orderNumber': %Char(OrderDS.number));
yajl_endObj();
yajl_addChar('success': 'true');
yajl_endObj();
SendResponse(OrderDS);
yajl_genClose();
EndSr;
Procedures invoked:
Dcl-Proc getInput;
Dcl-PI *n ind;
method varchar(10);
errmsg varchar(500);
End-PI;
Dcl-PR getenv pointer extproc(*dclcase);
var pointer value options(*string);
End-PR;
Dcl-S env pointer;
Dcl-S pos int(10);
Dcl-S url varchar(1000);
errMsg = '';
method = 'GET';
url = '';
// ------------------------------------------------------
// Retrieve the HTTP method.
// - Default to GET if not provided
// ------------------------------------------------------
env = getenv('REQUEST_METHOD');
if env <> *null;
method = %xlate(lower: UPPER: %str(env));
endif;
// ------------------------------------------------------
// Retrieve the URL
// - Should always be provided!
// ------------------------------------------------------
env = getenv('REQUEST_URI');
if env = *null;
errMsg = 'Unable to retrieve URL';
return *off;
else;
url = %xlate(UPPER: lower: %str(env));
endif;
return *on;
End-Proc;
Dcl-Proc GetJSONDocument;
Dcl-PI *N Ind;
OrderDS LikeDS(OrderDS_T);
DocNode Like(yajl_val);
End-PI;
Dcl-DS ErrorCode;
*n int(10) inz(0);
*n int(10) inz(0);
End-DS;
Dcl-PR getenv pointer extproc(*dclcase);
var pointer value options(*string);
End-PR;
//Dcl-S contentLength Int(10); //Moved definition to be at program level
Dcl-S fd Int(10);
Dcl-S inpBuf Pointer;
Dcl-S inpLength Int(10);
//Dcl-S outBuf Char(65535) Based(p); //Moved definition to be at program level
Dcl-S outString Char(500) Inz;
//Dcl-S p Pointer; //Moved definition to be at program level
Dcl-C CR x'25';
Dcl-C CRLF x'0d25';
Dcl-C LF x'0D';
Dcl-C O_WRONLY 2;
Dcl-C O_TEXTDATA 16777216;
//Clear outString;
DocNode = yajl_stdin_load_tree(*On: ErrMsg);
If ErrMsg <> '';
OrderDS.HttpStatus = 500;
OrderDS.ErrorMsg = 'json parse: ' + errMsg;
OrderDS.Success = *off;
EndIf;
If HTTPDebug = True Or OrderDS.ErrorMsg <> *Blanks;
Pathname = %trim(Folderpathin);
InpFileName = %Trim(programName) + '_' + %Char(JobNbr) + '_' +
%Char(%Timestamp()) + '.txt';
ContentLength = *Zeros;
p = getenv('CONTENT_LENGTH');
If p <> *null;
ContentLength = %Int(%Str(p));
EndIf;
If ContentLength > 0;
ReadIn( %Addr(inpBuf)
: ContentLength
: inpLength
: errorCode);
CmdString = 'STRQSH CMD(' + Q + 'touch -C 1252 ' +
%Trim(Pathname) + '/' + %Trim(InpFileName) + Q + ')';
CmdLength = %Len(%Trim(CmdString));
Monitor;
RunCommand(CmdString: CmdLength);
On-Error;
EndMon;
fd = Open(%Trim(Pathname) + '/' + %Trim(InpFileName): O_WRONLY+O_TEXTDATA);
If fd >= 0;
monitor;
ContentLength = yajl_stringify( DocNode
: p
: %Size(outBuf));
WriteOut(fd: %Trim(outBuf): ContentLength);
on-error;
endmon;
CloseF(fd);
EndIf;
EndIf;
EndIf;
Return OrderDS.Success;
End-Proc;
Dcl-Proc sendResponse;
Dcl-PI *n ind;
OrderDS likeds(OrderDS_t) const;
End-PI;
Dcl-S errMsg VarChar(500) Inz('');
errMsg = OrderDS.errorMsg;
yajl_writeStdout(OrderDS.httpStatus: errMsg);
return *On;
End-Proc;