OutgoingMessage
The 4D.OutgoingMessage
class allows you to build messages to be returned by your application functions in response to REST requests. If the response is of type 4D.OutgoingMessage
, the REST server does not return an object but the object instance of the OutgoingMessage
class.
Típicamente, esta clase puede ser usada en funciones declaradas con la palabra clave onHttpGet
y diseñadas para manejar las peticiones HTTP GET. Such requests are used, for example, to implement features such as download file, generate and download picture as well as receiving any content-type via a browser.
An instance of this class is built on 4D Server and can be sent to the browser by the 4D REST Server only. This class allows to use other technologies than HTTP (e.g. mobile).
Historia
Lanzamiento | Modificaciones |
---|---|
20 R7 | Clase añadida |
Ejemplo
In this example, a getFile()
function is implemented in the Datastore class and can be called by a REST request. The purpose is to return a testFile.pdf file as a response to the request:
Class extends DataStoreImplementation
exposed onHTTPGet Function getFile() : 4D.OutgoingMessage
var $result:=4D.OutgoingMessage.new()
var $file:=File("/RESOURCES/testFile.pdf")
$result.setBody($file.getContent()) // This is binary content
$result.setHeader("Content-Type"; "application/pdf")
return $result
Objeto OutgoingMessage
4D.OutgoingMessage objects provide the following properties and functions:
body : any el cuerpo del mensaje saliente |
headers : Object los encabezados actuales del mensaje saliente en forma de pares clave/valor |
.setBody( body : any ) sets the outgoing message body |
.setHeader( key : Text ; value : Text ) define lla llave key del encabezado del mensaje saliente con el valuer suministrado |
.setStatus( status : Integer ) establece la propiedad status con el status |
status : Integer el estado actual del mensaje saliente |
A 4D.OutgoingMessage object is a non-sharable object.
.body
body : any
Descripción
La propiedad .body
contiene el cuerpo del mensaje saliente. The following data types are supported in the .body
property:
- text
- blob
- object
- image
La propiedad .body
es de lectura-escritura.
También puede definir la propiedad .body
utilizando la función setBody()
, en cuyo caso el encabezado content-type
se define automáticamente.
.headers
headers : Object
Descripción
La propiedad .headers
contiene los encabezados actuales del mensaje saliente en forma de pares clave/valor.
La propiedad .headers
es de sólo lectura. Para definir un encabezado, use la función setHeader()
.
.setBody()
.setBody( body : any )
Parámetros | Tipo | Descripción | |
---|---|---|---|
body | any | -> | Body of the outgoing message |
Descripción
The .setBody()
function sets the outgoing message body.
The following data types are supported in the body:
- Text
- Blob
- Object
- Imagen
When this function is used, the content-type header is automatically set depending on the body type:
- Content-Type:text/plain if the body is a Text
- Content-Type:application/octet-stream if body is a Blob
- Content-Type:application/json if body is an Object
- Content-Type:image/jpeg, image/gif... si el body es una imagen
If body is not of a supported value type, an error is returned.
.setHeader()
.setHeader( key : Text ; value : Text )
Parámetros | Tipo | Descripción | |
---|---|---|---|
key | Text | -> | Propiedad de encabezado a definir |
value | Text | -> | Valor de la propiedad del encabezado |
Descripción
La función .setHeader()
define lla llave key del encabezado del mensaje saliente con el valuer suministrado. If both parameters are not Text values, an error is raised.
When returning a 4D.OutgoingMessage object instance, 4D automatically sets some headers (e.g. Set-Cookie
with WASID4D=...
and 4DSID__ProjectName_=....
).
If you set a value for the "Content-Type" header key, make sure you call this function after the call to setBody()
, because setBody()
automatically fills this header. For a list of "Content-Type" header values, please refer to the WEB SEND BLOB
documentation.
.setStatus()
.setStatus( status : Integer )
Parámetros | Tipo | Descripción | |
---|---|---|---|
status | Integer | -> | Estado a definir |
Descripción
La función .setStatus()
establece la propiedad status
con el status.
If status is not an integer value, an error is raised.
For a list of HTTP status codes, please refer the HTTP status code list on Wikipedia.
.status
status : Integer
Descripción
La propiedad .status
contiene el estado actual del mensaje saliente. This property can be set using the setStatus()
function.