Gert Vanthienen's blog

Tuesday, May 27, 2008

Sending HTTP response code in ILE RPG

We (Tim Wyers and I) just spend a few hours to find out how to send an HTTP response code from an ILE RPG CGI program on System i. From the documentation provided by IBM, it looked like the QzhbCgiUtils API was the way to go, but we ended up with a this very simple solution: If you just write Status: 404 Not found to standard out, the HTTP server will translate that into the HTTP/1.1 404 Not found response code.

d WriteStdout pr ExtProc('QtmhWrStout')
d 65535a Const Options(*Varsize) //data variable
d 9b 0 Const //length of data
d 16a Options(*Varsize) //error struct
d/Copy Qsysinc/Qrpglesrc,Qusec
/free
WriteStdout('Status: 404 Not Found' + X'15': 25: Qusec);
WriteStdout('Content-type: text/html'+X'15'+X'15': 27: Qusec);
WriteStdout('<html\><body>Page not found</body></html>': 29: Qusec);
Return;
/end-free

Labels: ,

2 Comments:

  • Thanks for this elegant tip. I had been wrestling with the QzhbCgiUtils to find a way to send a 204 No Content header. Your technique worked like a charm!

    By Blogger Unknown, At June 12, 2009 at 1:54 PM  

  • I was trying to use REST for RPG CGI and I needed to return a true http 404 thanks

    Don Denoncourt
    dondenoncourt@gmail.com

    By Blogger Unknown, At April 3, 2010 at 8:54 PM  

Post a Comment

Subscribe to Post Comments [Atom]



<< Home