Mitch Scobell · Jul 23, 2018

Caché CSP REST <MAXSTRING> error when writing back the response

I'm trying to have my REST service return the entire data set for one of our legacy globals.  Currently I am parsing the object from a SQL statement into generic objects to be returned like this:

         Set specimenArray=[]
        WHILE resultSet.%Next() {
            Set specimen = {}
            Set specimen.SpecimenId= resultSet.SpecimenId
            Set specimen.ProjectId = resultSet.ProjectId
            Set Oref = specimenArray.%Push(specimen)
        Set tProxy = specimenArray.%ToJSON()
        set %response.Status = 200
        set %response.ContentType = "application/json"
        Set %response.CharSet="utf-8"

        Write tProxy

However, since the dataset is massive, it returns a <MAXSTRING> error.  When I limit the SQL to "TOP 500" it works perfectly and  I am certain I need to do some sort of stream back, but unsure of how to do that.  Any pointers of how I can take this array and stream the JSON back?


0 624
Discussion (2)2
Log in or sign up to continue

When you call %ToJSON() in a DO context, it writes to the current device, which should not cause a MAXSTRING error. However, due to an interaction with I/O redirection, you should preface the call with WRITE "":

write ""
do specimenArray.%ToJSON()

This may be fixed in recent versions.

That worked perfectly.  Thank you so much!