Published on InterSystems Developer Community (https://community.intersystems.com)

Home > Send an image as a binary from a REST Service, as %Stream.GlobalBinary or %Stream.FileBinary;

Question
Yone Moreno · Jan 28, 2021

Send an image as a binary from a REST Service, as %Stream.GlobalBinary or %Stream.FileBinary;

Hello,

We would need some help,

Our aim is to send an image as binary data using a REST Service

Currently we do the following:

1 We get from the external system a binary image in our REST Operation

2 We encode it to Base64

set linea=""
    while (tResponse.Data.AtEnd = 0) {
        set linea = linea_$system.Encryption.Base64Encode(tResponse.Data.Read(57))
    }

3 We send it to the Process in a Response Message:

Class Mensajes.Response.miSCS.ConsultarImagen Extends Ens.Response
{

Property resultado As %Boolean;

Property informacion As %String(MAXLEN = "");

Property binario As %Stream.GlobalBinary;

// Property binario As %Stream.FileBinary;

// Property binario As %GlobalCharacterStream;

// Property binario As %Stream.TmpCharacter;

Property error As EsquemasDatos.Seguridad.Error;

4 The Process takes kindly the Base64 and converts it into a Binary:

set binario = $system.Encryption.Base64Decode(response.informacion)
 
set response.informacion = binario

5 Our REST Service gets the Process response:

//Enviamos al Proceso
        #dim objetoSalida as Mensajes.Response.miSCS.ConsultarImagen
        set tSC = ..SendRequestSync("miSCS",objetoEntrada,.objetoSalida)

6 It writes the binary saved in "objetoSalida.informacion" to the binary property "objeto.binario"

 

       do objetoSalida.binario.Write(objetoSalida.informacion)

        $$$LOGINFO("objetoSalida.binario: "_objetoSalida.binario.Read())
        do objetoSalida.binario.Rewind()
        
        
        set objetoSalida.informacion = ""

7 Here comes the challenge:

When we convert the Object to JSON, it outputs "binario" the binary data, as "null":

set tSC = claseAux.%WriteJSONStreamFromObject(.pOutput,.objetoSalida,,,,"aeloqtuw")
        
        
$$$LOGINFO("pOutput.Read(): "_pOutput.Read())
do pOutput.Rewind()

 

When we observe the Message Viewer we see the following traces:

1 Before, we see objetoSalida.binario binary data, the image:

2 After, we observe it as null:

 

So then, in Postman we see it null:

 

 

How could we send an image as a binary from a REST Service?

 

We would need to send it as binary data, because if we return it as: %GlobalCharacterStream;

 

Postman would have the JPG image data with strange characters as "\x00" or "\x01"

 

Thanks for your help,

We have read:

https://community.intersystems.com/post/how-save-png-image-cache-base64-...

https://community.intersystems.com/post/decode-base64-file

https://community.intersystems.com/post/handling-images-cach%C3%A9-json-...

https://community.intersystems.com/post/binary-support-json

https://community.intersystems.com/post/sql-query-returns-x00

https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY...

https://community.intersystems.com/post/stream-binary-json-work-around-h...

#Code Snippet #Ensemble

Source URL:https://community.intersystems.com/post/send-image-binary-rest-service-streamglobalbinary-or-streamfilebinary