I've been trying to do this for a long time. I've read too much, tried too many things, and still was left wondering. Well, with a little directional help from Sjef Bosman, and a ton from ChapGPT, here's my solution for doing a POST of data in JSON format:
Sjef, thank you for pointing me in a direction that worked in the end. With the extensive help of ChatGPT, here's the solution that I hit upon. It both allows for a seeming unlimited POST amount, well, enough for what I need, and my end agent can process the POST as it needs it.
XPage, called “JSON”
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" viewState="nostate">
<xp:this.beforePageLoad><![CDATA[#{javascript:
var externalContext = facesContext.getExternalContext();
var request = externalContext.getRequest();
var response = externalContext.getResponse();
if (request.getMethod().equalsIgnoreCase("POST")) {
var inputStream = request.getInputStream();
var reader = new java.io.BufferedReader(new java.io.InputStreamReader(inputStream));
var buffer = new java.lang.StringBuffer();
var line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
// Use the Java class to save the POST body as Rich Text
var postBody = buffer.toString();
var formName = "JSON"; // The form name you're using
var dbPath = database.getFilePath(); // The path to your database
var unid = richTextHandler.RichTextHandler.savePostBodyAsRichText(session, dbPath, formName, postBody);
var agentURL;
if (unid) {
agentURL = "/jsonapi.nsf/jsonpost?OpenAgent&UNID=" + unid;
} else {
agentURL = "/jsonapi.nsf/jsonpost?OpenAgent&UNID=ERROR";
}
externalContext.redirect(agentURL);
facesContext.responseComplete(); // Signal that the response is complete and no further rendering should occur
}
}]]></xp:this.beforePageLoad>
</xp:view>
Form, called “JSON”
It includes 1 rich text field called “POSTBODY”
Agent, called "(JSON API POST")"
%REM
Agent JSON API Test
Created
Description: Comments for Agent
%END REM
OPTIONS >>
Option Public
Option Declare
Use "JSON Library"
DECLARATIONS >>
Dim parser As JSONParser
Dim jsonObj As JSONObject
Dim jsonArr As JSONArray
Dim jsonString As String
Sub Initialize
Set session = New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim rtItem As NotesRichTextItem
Dim docUNID As String, authKey As String, runStatus As String
Dim jsonString As String
On Error GoTo ErrorOut
runStatus = "Start:"
Set uidoc = session.DocumentContext
Set db = session.CurrentDatabase
Set cgidoc = CGI
runStatus = runStatus + "Set CGI Doc:"
REM Pull CGI variables into variables.
If Not(cgidoc Is Nothing) Then
If cgidoc.HasItem( "UNID" ) Then
docUNID = CStr(cgidoc.UNID(0))
End If
End If
runStatus = runStatus + "Set Doc:"
If docUNID <> "" Then
Set doc = db.Getdocumentbyunid(docUNID)
End If
runStatus = runStatus + "Get POSTBODY String:"
If doc.HasItem( "POSTBODY" ) Then
runStatus = runStatus + "Has the Field:"
Set rtItem = doc.GetFirstItem( "POSTBODY" )
runStatus = runStatus + "RTItem is OK (" + CStr(Not(rtItem Is Nothing)) + "):"
'jsonString = rtItem.GetUnformattedText
'Set parser = New JSONParser
'Set jsonObj = parser.parse(jsonString)
'If jsonObj.HasItem( "authkey" ) Then
' authKey = jsonObj.GetItem( "authkey" )
'End If
End If
REM Get the userID by username and password encrypted
' If jsonObj.HasItem( "username" ) Then
' uName = jsonObj.GetItem( "username" )
' End If
' If jsonObj.HasItem( "password" ) Then
' uPass = jsonObj.GetItem( "password" )
' End If
'authKey = CStr(uidoc.Path_Info(0))
REM Return JSON response.
Print |Content-type: text/plain|
Print "{"
Print " ""AuthKey"": """ + authKey + ""","
Print " ""RunStatus"": """ + CStr(runStatus) + ""","
Print " ""UNID"": """ + CStr(docUNID) + ""","
If (doc Is Nothing) Then
Print " ""STATUS"": ""Failure"""
Else
Print " ""STATUS"": ""Success"""
End If
Print "}"
Exit Sub
ErrorOut:
runStatus = runStatus + "An Error Occurred (" + Str(Err) + "): " + cstr(Error$)
Print |Content-type: text/plain|
Print "{"
Print " ""AuthKey"": """ + authKey + ""","
Print " ""RunStatus"": """ + CStr(runStatus) + ""","
Print " ""UNID"": """ + CStr(docUNID) + ""","
Print " ""STATUS"": ""Failure"""
Print "}"
End Sub
Script Library, called “JSON Library”.
It can be found at this location: https://www.openntf.org/main.nsf/project.xsp?r=project/JSON+LotusScript+Classes
JAVA Class, called “RichTextHandler”
package richTextHandler;
import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.NotesException;
import lotus.domino.RichTextItem;
import lotus.domino.Session;
public class RichTextHandler {
public static String savePostBodyAsRichText(Session session, String dbPath, String formName, String postBody) {
Database db = null;
Document doc = null;
try {
db = session.getDatabase("", dbPath, false);
if (db != null) {
doc = db.createDocument();
doc.replaceItemValue("Form", formName);
RichTextItem rtItem = doc.createRichTextItem("POSTBODY");
rtItem.appendText(postBody);
if (doc.save(true, false)) {
return doc.getUniversalID();
}
}
} catch (NotesException e) {
e.printStackTrace();
} finally {
try {
if (doc != null) doc.recycle();
if (db != null) db.recycle();
} catch (NotesException e) {
// Handle recycle exceptions if necessary
}
}
return null;
}
}
With all this in place you can now POST to a url: https://www.yourdomain.com/jsonapi.nsf/JSON.xsp and it will save the POST to a document. It then calls your JSON agent with the document UNID so you can process it and/or return a JSON result to the sending API.