Send an image as a multi part File
Hello,
We would like some advice
We have developed a REST Operation
The code is:
Class Operaciones.REST.miSCS.miSCS Extends EnsLib.REST.Operation
{
Parameter INVOCATION = "Queue";
/// 📤 Subir la imagen del usuario
Method SubirImagen(pRequest As Mensajes.Request.miSCS.SubirImagen, pResponse As Mensajes.Response.miSCS.SubirImagen) As %Library.Status
{
set httpRequest = ##class(%Net.HttpRequest).%New()
set tResponse = ##class(%Net.HttpResponse).%New()
//set httpRequest.ContentType = "application/json"
set httpRequest.ContentType = "multipart/form-data"
set url = ##class(Util.TablasMaestras).getValorMaestra("MISCS.PARAMETRIZACIONES","url")
set path = ##class(Util.TablasMaestras).getValorMaestra("MISCS.PARAMETRIZACIONES","path")
set servicio = ##class(Util.TablasMaestras).getValorMaestra("MISCS.PARAMETRIZACIONES","servicio")
set recurso = ##class(Util.TablasMaestras).getValorMaestra("MISCS.PARAMETRIZACIONES","subirImagen")
set URL = "http://"_url_path_servicio_recurso
$$$LOGINFO("URL: "_URL)
/*
//Probamos a leer una imagen local y a enviarla
Set stream=##class(%Stream.FileCharacter).%New()
Set sc=stream.LinkToFile("Pictures\mi.jpg")
While 'stream.AtEnd {
Set linea=stream.Read()
}
$$$LOGINFO("linea: "_linea)
*/
set tFormVarNames = "expediente,imagen"
set tData("expediente") = pRequest.expediente
set tData("imagen") = pRequest.imagen
//set tData("imagen") = linea
set tSC = ..Adapter.SendFormDataArray(.tResponse,"POST",httpRequest,.tFormVarNames,.tData,URL)
$$$LOGINFO("tSC: "_tSC)
if $$$ISERR(tSC){
$$$ThrowOnError(tSC)
}
if (tResponse.Data.AtEnd = 0) {
$$$LOGINFO("En SubirImagen, tamaño de tResponse: "_tResponse.Data.Size)
set linea = tResponse.Data.Read()
$$$LOGINFO("Linea: "_linea)
}
set pResponse = ##class(Mensajes.Response.miSCS.SubirImagen).%New()
do pResponse.return.Write(linea)
Quit pResponse
}
XData MessageMap
{
<MapItems>
<MapItem MessageType="Mensajes.Request.miSCS.SubirImagen">
<Method>SubirImagen</Method>
</MapItem>
</MapItems>
}
When we send the request from the Production testing the Operation we get the following exception:
ERROR <Ens>ErrException: <THROW>zSubirImagen+27^Operaciones.REST.miSCS.miSCS.1 *%Exception.StatusException ERROR <Ens>ErrHTTPStatus: Received non-OK status 500 from remote HTTP server: 'HTTP/1.1 500 ' -- logged as '-' number - @' Set sc=tSC Throw:('sc) ##class(%Exception.StatusException).ThrowIfInterrupt(sc)'
Using POSTMAN directly to the endpoint we observe the following in Whireshark:
> As you would notice we see that the image is being sent as a multipart file, so then it is correct because the endpoint replies to us.
When we capture the request sent with Ensemble we see:
Here we observe that the image is being sent as text.
How could we send an image as a multi part File, using a REST Operation in Ensemble?
Could you help us? Could you provide some code example, please?
Thanks in advance
We have read:
https://docs.intersystems.com/latest/csp/docbook/Doc.View.cls?KEY=EREST_...
https://community.intersystems.com/post/polling-external-rest-api-ensemble
https://community.intersystems.com/post/how-send-file-json
https://community.intersystems.com/post/send-json-http-request
https://docs.intersystems.com/latest/csp/documatic/%25CSP.Documatic.cls?...
https://docs.intersystems.com/latest/csp/docbook/Doc.View.cls?KEY=GOBJ_p...