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 custom HTTP request handler functions or 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).
História
Release | Mudanças |
---|---|
20 R7 | Classe adicionada |
Exemplo
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
OutgoingMessage Object
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 |
Um objeto 4D.OutgoingMessage é um objeto não compartilhável.
.body
body : any
Descrição
The .body
property contains the outgoing message body. The following data types are supported in the .body
property:
- text
- blob
- object
- image
The .body
property is read-write.
Você também pode definir a propriedade .body
usando a função setBody()
, caso em que o cabeçalho content-type
é automaticamente definido.
.headers
headers : Object
Descrição
The .headers
property contains the current headers of the outgoing message as key/value pairs.
The .headers
property is read-only. Para definir um cabeçalho, use a função setHeader()
.
.setBody()
.setBody( body : any )
Parâmetro | Tipo | Descrição | |
---|---|---|---|
body | any | -> | Body of the outgoing message |
Descrição
The .setBody()
function sets the outgoing message body.
The following data types are supported in the body:
- Text
- Blob
- Object
- Imagem
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âmetro | Tipo | Descrição | |
---|---|---|---|
| | Text | -> | Header property to set |
value | Text | -> | Value of the header property |
Descrição
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âmetro | Tipo | Descrição | |
---|---|---|---|
status | Integer | -> | Status para definir |
Descrição
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
Descrição
The .status
property contains the current status of the outgoing message. Essa propriedade pode ser definida com a função setStatus()
.