#XPRIM_JsonWriter & WriteBase64StringFromFile

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
Post Reply
adale
Posts: 132
Joined: Wed Apr 08, 2020 9:18 pm
Location: Poplarville, MS

#XPRIM_JsonWriter & WriteBase64StringFromFile

Post by adale » Fri Feb 23, 2024 5:23 am

Scenario:
VL web application for communicating with business partner REST API application (http put). Our server is IBM i.
New requirement from partner is to upload file in base64 in the http put request.
I read in other posts that the WriteBase64StringFromFile is good up to 16Meg, due to limitation on the IBM i. I am limiting the selected file to 10Meg to ensure we stay under this limit.

Previous request with base64 strings were fairly small, so no issues. But now converting the file to base64 string makes the message rather long:
DEFINE_COM Class(#XPRIM_HttpRequest) Name(#HttpRequest) Scope(*Application)
DEFINE_COM Class(#XPRIM_JsonWriter) Name(#Writer)

#Writer.WriteBase64StringFromFile Name('Document') Filepath(#IXI_FILEBLOB)
* This works, and doing a bit of debugging I can verify the content is built.

My issue is that I get an http 400 error when I try to execute http put request. Looking at the http response, it states the elements document and root are not closed. Trying to catch the outbound request data, it looks like the request is getting truncated at it is sent out.

I am thinking my issue is the command to add the content:
* Add completed string to the request
#HttpRequest.Content.AddString Value(#Writer.AsString)

with the .AddString intrinsic, is this really using the attributes of *String so a limit of 65535 ?

How, or what would be the appropriate way to generate the base64string and include it in the http get request (not as an attachment)?
Arlyn Dale
Servias LLC

BrendanB
Posts: 116
Joined: Tue Nov 24, 2015 10:29 am

Re: #XPRIM_JsonWriter & WriteBase64StringFromFile

Post by BrendanB » Wed Feb 28, 2024 9:37 am

Arlyn,

indeed 65535 chars is the point at which your request will be truncated (on IBMi only).

1 option is to use the StreamWriters to write your request to a file on the IFS, and then use the JSM 'HTTPService' to send the file.
Another option is of course to use JSM to construct the request as well..

Code: Select all

Define_Com Class(#PRIM_IOC.FileStream) Name(#FileStream)
Define_Com Class(#PRIM_IOC.StreamWriter) Name(#StreamWriter) Stream(#FileStream)
Define_Com Class(#PRIM_JSON.Writer) Name(#JsonWriter) TextWriter(#StreamWriter)
note: the #JsonWriter has methods similar to the #XPRIM_JsonWriter...

Brendan.

adale
Posts: 132
Joined: Wed Apr 08, 2020 9:18 pm
Location: Poplarville, MS

Re: #XPRIM_JsonWriter & WriteBase64StringFromFile

Post by adale » Thu Mar 14, 2024 9:46 am

I had submitted the length question to Lansa support, but have yet to receive any reply, but Brendan answered the initial question.

At this time, I am trying to avoid having to use Integrator JSM, and keep it all in the simplified VL Web coding.

Since the limitation I am dealing with is the 65535 character length string, when using WriteString on the IBM i, I have tried a couple other options.
The closest I can get is to use #XPRIM_JSONObject.InsertString

This works well from most all strings and will insert strings longer than 65535, except for the one that I need which is: WriteBase64StringFromFile.
I can not find a XPRIM_JsonObject for WriteBase64StringFromFile ?

This will build the Json request to send:

Code: Select all

DEFINE_COM Class(#XPRIM_JsonWriter) Name(#Writer2)
DEFINE_COM Class(#XPRIM_JsonObject) Name(#JsonObject)

#Writer2.BeginObject
#Writer2.WriteBase64StringFromFile Filepath(#IXI_FILEBLOB)
#Writer2.EndObject
#JsonObject.InsertString Key('Document') String(#Writer2.AsString)
But, it has the the Document nested in the Json body, with an extra set of Json object tags "{\"\":. Although it ends up being valid Json, it gives the trade partner grief, and their system kicks it out as an error.

I have tried using the #JsonObject GetString to try and read the string from the #Writer2, but no luck, the document value is blank when the Json body is sent.

Code: Select all

#JsonObject.GetString Key('Document') Result(#doc)
#JsonObject.InsertString Key('Document') String(#doc)
Any suggestions, or help, on how to get the large string from "WriteBase64StringFromFile" into a simple JsonObject (key / string) is appreciated.
Arlyn Dale
Servias LLC

Tim McEntee
Posts: 43
Joined: Thu May 26, 2016 8:46 am

Re: #XPRIM_JsonWriter & WriteBase64StringFromFile

Post by Tim McEntee » Tue Mar 19, 2024 3:44 pm

Hi Arlyn

I have no idea if this will work. It does in Postman and in Integrator

XPRIM_HttpContent has an Addfile method. If you write your very long string to a file, then add the file using this method I think it is worth a try.

Tim

adale
Posts: 132
Joined: Wed Apr 08, 2020 9:18 pm
Location: Poplarville, MS

Re: #XPRIM_JsonWriter & WriteBase64StringFromFile

Post by adale » Thu Mar 21, 2024 6:34 am

Tim,
I tried working with the Addfile method, but didn't have much luck. Kept either having it blow up the request, or end up like the other request with extra sets of segment delimiters which the trade partner can't accept.

Another idea or question I have, is what about using #XPRIM_JsonWriter.SetOutputToHttpRequest instead of .SetOutputToString ?
Will the simply write the Json into the request, without having to deal with the String limitation of 65535?

Does anyone have a very simple example of using the .SetOutputToHttpRequest method?

My attempts so far have all been blank???
Arlyn Dale
Servias LLC

adale
Posts: 132
Joined: Wed Apr 08, 2020 9:18 pm
Location: Poplarville, MS

Re: #XPRIM_JsonWriter & WriteBase64StringFromFile

Post by adale » Fri Mar 22, 2024 6:33 am

I suspect that there is not (nor will be) a .WriteBase64StringFromFile function with XPRIM_JsonObject, as the docs state that XPRIM_JsonWriter is better suited to deal with larger data.

That being the case, I found a current solution to be to use:
#Writer.SetOutputToHttpRequest Httprequest(#HttpRequest)
instead of
#Writer.SetOutputToString

This works to write the Base64 string from file directly into the HttpRequest, and gets me past the 65535 limitation I was having running on the IBMi.
Note: When using .SetOutputToHttpRequest, the .Writer commands do not produce any String data that you could later use (debug, etc.), so the only option is to use trace logging if you need to review the Json data being built in your request.
Arlyn Dale
Servias LLC

Post Reply