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.
Typically, this class can be used in functions declared with the onHttpGet
keyword and designed to handle HTTP GET requests. 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 the outgoing message body |
headers : Object the current headers of the outgoing message as key/value pairs |
.setBody( body : any ) sets the outgoing message body |
.setHeader( key : Text ; value : Text ) sets the outgoing message header key with the provided value |
.setStatus( status : Integer ) sets the status property with the given status |
status : Integer the current status of the outgoing message |
Un objeto 4D.OutGoingMessage es un objeto no compartible.
.body
body : any
Descripción
The .body
property contains the outgoing message body. The following data types are supported in the .body
property:
- text
- blob
- object
- image
La propiedad .body
es de lectura-escritura.
You can also set the .body
property using the setBody()
function, in which case the content-type
header is automatically set.
.headers
headers : Object
Descripción
The .headers
property contains the current headers of the outgoing message as key/value pairs.
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... if body is an Image
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
The .setHeader()
function sets the outgoing message header key with the provided value. 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
The .setStatus()
function sets the status
property with the given 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
The .status
property contains the current status of the outgoing message. This property can be set using the setStatus()
function.