If you're encoding your data before sending it, you have to specify how it was encoded in a content encoding header so that the server you're sending data to knows how to decode it.

I think it's more likely, though, that it's an issue with your content type. Where you're setting it to "text/plain", if it's supposed to be json, you might try setting it to "application/json" instead.

If the file isn't accessible to link to directly, you may want to look into extending the %CSP.StreamServer class and linking to that. At a bare minimum, you'll want to override the OnPage and OnPreHTTP methods:

ClassMethod OnPage() As %Status
{
set myfile = ##class(%File).%New("/path/to/file")
do myfile.Open("S")
do myfile.OutputToDevice()
quit $$$OK
}

ClassMethod OnPreHTTP() As %Boolean [ Language = cache ]
{
do %response.SetHeader("Content-Disposition","attachment;filename=""filename""")
quit 1
}

Of course using your own file name and the path to the file. That's the local computer file path, not a URL. You also should set the content type appropriately using set %response.ContentType = "text/csv" or whatever the MIME type of the file is so that the browser can identify it correctly.

Unless you want to have to write another %CSP.StreamServer for every file, you'll have to pass the name of the filepath as an argument. So that would look more like:

ClassMethod OnPage() As %Status
{
set myfile = ##class(%File).%New(%request.Get("filepath"))
do myfile.Open("S")
do myfile.OutputToDevice()
quit $$$OK
}

ClassMethod OnPreHTTP() As %Boolean [ Language = cache ]
{
set filepath = %request.Get("filepath")
set %response.ContentType = "text/csv" //or whatever the appropriate MIME type is
do %response.SetHeader("Content-Disposition","attachment;filename="""_$P(filepath,"/",*)_"""")
quit 1
}

Then you could link to it as whatever the path to your stream server is with ?filepath=/path/to/file on the end.

If you take that approach, though, do some validation on the filepath and make sure it can ONLY go to the folder you want! Or, only pass the filename as a parameter to the page, and hard-code the folder in those methods.

Are you just trying to get the json contained in a character stream into a string a vice versa? If so, just read and write to and from the stream:

set json = ""
while 'stream.AtEnd
{
    set json = json_stream.Read()
}

That should get you the contents of the stream into a string.

do stream.Write(json)

That should write the json to a stream.

Or is that not what you're trying to do?

Here is some useful documentation.

You're going to want to make a class that extends %CSP.REST and set up an application that uses that class as its dispatch class. You'll have a URL map in that class to tell IRIS or Cache what to do with the request. Depending on your specific application, you might also want to get familiar with using %request and %response in that process.

Rochdi, when you say you've figured out how to save it to C:\Temp\filename.csv, are you saying you have it saved on the client, or on the server?

If it's saving the file on the server, you can create a class that extends %CSP.StreamServer, then override the OnPreHTTP and OnPage class methods like this for simple text files:

 Class fileserver Extends %CSP.StreamServer
{ 
ClassMethod OnPreHTTP() As %Boolean
{
do %response.SetHeader("Content-Type","text/csv")
do %response.SetHeader("Content-Disposition","attachment;filename=""myfile.csv""")
quit 1
} 
ClassMethod OnPage() As %Status
{
set file = ##class(%File).%New("/path/to/file.csv")
do file.Open("R")
while file.AtEnd '= 1{
write file.ReadLine(),!
}
quit $$$OK
} 
}

and then just link to it that page to download.

Once you get to things that aren't plain text, it gets a little more complicated, but this should work for a simple csv.

Your result set should be an EnsLib.SQL.GatewayResultSet, which has a method called GetSnapshot(). That method has you pass a EnsLib.SQL.Snapshot by reference. You're probably going to want to set the FetchAll parameter on the GetSnapshot() method to 1 so it gets all the results, but you can also create your EnsLib.SQL.Snapshot before using GetSnapshot() and set it's starting row and max rows if you'd like. Then you can iterate over the snapshot instead of the result set. Once you've gone through it once, you could either create a new snapshot by calling the GetSnapshot() method again, or you can use the snapshot's Rewind() method.

According to this page, if it's something that still in development, you can use the CleanProduction() method to clear the message queues. Using it in a live system isn't recommended because it clears out everything pretty indiscriminately, but it's useful for debugging.

Productions get the suspend status when after shutting down there are still synchronous messages that could not be processed.