Page 1 of 1

#XPRIM_JsonWriter - SetOutputToHttpRequest

Posted: Tue Jan 11, 2022 3:47 am
by Speedlime
Afternoon

I am calling a external API, passing in some Json and getting Json back
I am writing the Json directly to the http request see below

Code: Select all

* Add the constructed JSON to the request body
#Writer.SetOutputToHttpRequest HttpRequest(#request)

* Construct the JSON IN
* Root
#Writer.BeginObject
#Writer.WriteString Name('CustomerNumber') Value('WD0009')
#Writer.WriteString Name('ProductGroup') Value('VBRO')
#Writer.WriteString Name('ProductSize') Value('EAAA')
* End Root Object
#Writer.EndObject
Is there a way I can get to the Json that is been written to the request. ?? either a string or written to a file ?? To check the format and data is correct

I can duplicate the above code and do the following

Code: Select all

* Save as String for debug
#Writer.SetOutputToString String(#MyJsonStringIn)
#Writer.BeginObject
#Writer.WriteString Name('CustomerNumber') Value('WD0009')
#Writer.WriteString Name('ProductGroup') Value('VBRO')
#Writer.WriteString Name('ProductSize') Value('EAAA')
* End Root Object
#Writer.EndObject
In debug I can see the string and I can copy to check the format. Seems over kill.

I can read the response like this, and get the required values I need.

Code: Select all

* Request OK
If (#request.Response.IsSuccessfulRequest)
* Status OK 200
If (#request.Response.IsSuccessHttpStatusCode)
* Json Response
#Reader.SetSourceHttpResponse HttpResponse(#request.Response)

#M6CURR := #Reader.ReadStringWithName( "Currency" ).AsNativeString
#STD_PRICE := #Reader.ReadStringWithName( "Price" ).AsNumber

Endif
Endif

Basically I want to get all the Json in the request and the response either in a string or a file, is there a better way other than using the Json Writer to read and write it to a unicodestring variable.
Reason is for logging what went in and what came back either to a audit record (with string value) or to a file on the PC or IFS.

Regards

Re: #XPRIM_JsonWriter - SetOutputToHttpRequest

Posted: Tue Jan 11, 2022 6:58 am
by davidbalansa
You can get the request information if you turn on logging. The following post explains how:

viewtopic.php?f=3&t=2499

Direct link to the LANSA documentation:

https://docs.lansa.com/14/en/lansa018/i ... 1_0395.htm

Hope it helps.

Re: #XPRIM_JsonWriter - SetOutputToHttpRequest

Posted: Wed Jan 19, 2022 8:56 pm
by Speedlime
Thanks for this, I will set up the logging and see what I get.

Re: #XPRIM_JsonWriter - SetOutputToHttpRequest

Posted: Fri Mar 22, 2024 12:13 am
by adale
Does anyone have any updated documentation or notes on the ".SetOutputToHttpRequest" method?

When using XPRIM_JsonWriter.SetOutputToString, I can build the Json string with (#HttpRequest.Content.AddSTring . . . ) and it works to complete the request, so I confirmed the Json building/format is valid. Logging is turned on, and I can verify the HTTPRequest log files (requests and response).

But if I change to XPRIM_JsonWriter.SetOutputToHttpRequest, I get nothing?
Logging still on, but nothing in the HTTPRequest log files?
My assumption is that I am missing some last command like the .Content.AddString but for the HttpRequest ?

Code: Select all

#Writer.SetOutputToHttpRequest Httprequest(#HttpRequest)

* Begin root
#Writer.BeginObject

#Writer.WriteString Name('accountNum') Value(#ACCTN)

#Writer.WriteString Name('DocumentName') Value(#IXI_DOCNAME)

#Writer.WriteString Name('TransactionReference') Value(#IXI_TREF)

#Writer.WriteString Name('DocType') Value(#IXI_DOCTYPE)

* IBM limitation of Add String = 65535, it will truncate the file
* This will only work for very small files.
* #Writer.WriteString Name('Document') Value(#IXI_FILEBLOB)
* Send in base64 format
#Writer.WriteBase64StringFromFile Name('Document') Filepath(#IXI_FILEBLOB)

#Writer.WriteString Name('Identification') Value('Null')

* end root
#Writer.EndObject

* ----- *
* Execute the PUT REST API
* ----- *
#HttpRequest.DoPut Url(#Url)

Re: #XPRIM_JsonWriter - SetOutputToHttpRequest

Posted: Fri Mar 22, 2024 5:43 am
by adale
Have found that if I leave out the lines adding the file string, this will work.
There is some issue with trying to use the .WriteBase64StringFromFile command, with .SetOutputToHttpRequest.
I have submitted a support case, and will update once I get a resolution.

Re: #XPRIM_JsonWriter - SetOutputToHttpRequest

Posted: Fri Mar 22, 2024 6:25 am
by adale
I found the usual enemy suspect, me!
With the .SetOutputToHttpRequest function, the .WriteString commands write directly to the HttpRequest, and not an actual String.
I had a command later in my debug routines (which work great with .SetOutputToString), that was failing trying complete a Writer.asString command.
This is why I had no trace logs, found the issue in the VL x_err.log file.