Reading large stream from %CSP.Request
We have created a number of REST adapters that extend %CSP.REST and these have been working fine. However, we are now facing a problem with a new one where very large XML documents are being posted. This is the pattern we are following:
Set tSC = ##class(Ens.Director).CreateBusinessService("Epro_ItkHttp",.tBusinessService)
Set tSC = tBusinessService.ProcessInput(inMsg, .tBusinessServiceOutput)
However, when the %request is particularly long, it is being truncated, and working with %XML.Reader then fails in the business service class.
I believe the call to Read() is the problem. If I call it with some arbitrarily long value - such as Read(500000) - then it works.
What is the correct approach here? Is there any harm in putting in a massive number into the len parameter of the Read() method? Or is there a way of looping through the stream and constructing the object to pass to the business service?
Any advice much appreciated.
I believe there is a default length for the Read. If you pass in a number large enough or larger, it will read up to the length you specify. I see no harm in having a very large number.
You cannot read more than 3641144 characters from a stream to a string, as this is the limit for long strings: String Length Limit
From the documentation for the Read() method:
So you can write like this:
Set inMsg = %request.Content.Read($$$MaxLocalLength)
Macros are defined in %msql.inc:
Thanks for your reply, makes complete sense. I wasn't aware of $$$MaxLocalLength.
On looking into this further, it turns out I don't need to Read() at all. Just passing the %request.Content on like this:
Set tSC = tBusinessService.ProcessInput(%request.Content, .tBusinessServiceOutput)
Does the trick. Then in the business service class, using the OpenStream() method of %XML.Reader behaves as expected when deserialising to the class objects.
You can check for the size of the %request content reading "%request.Content.Size" and read the exact amount of bytes but keep in mind that , as Vitaliy already said, as long as you read a stream into a string you have to deal with the string Max Length.