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 the Qodly server and can be sent to the browser by the REST Server only. This class allows to use other technologies than HTTP (e.g. mobile).
Example
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:
extends DataStoreImplementation
exposed onHTTPGet function getFile() : 4D.OutgoingMessage
var result = 4D.OutgoingMessage.new()
var vfile = file("/SOURCES/Shared/testFile.pdf")
result.setBody(vfile.getContent())
result.setHeader("Content-Type", "application/pdf")
return result
OutgoingMessage Object
4D.OutgoingMessage objects provide the following properties and functions:
A 4D.OutgoingMessage object is a non-sharable object.
.body
body* : any
Description
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.
You can also set the .body
property using the setBody()
function, in which case the content-type
header is automatically set.
.headers
headers* : object
Description
The .headers
property contains the current headers of the outgoing message as key/value pairs.
The .headers
property is read-only. To set a header, use the setHeader()
function.
.setBody()
.setBody*( body : any )
Parameter | Type | Description | |
---|---|---|---|
body | any | → | Body of the outgoing message |
Description
The .setBody()
function sets the outgoing message body.
The following data types are supported in the body:
- text
- blob
- object
- image
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 : string , value : string )
Parameter | Type | Description | |
---|---|---|---|
key | string | → | Header property to set |
value | string | → | Value of the header property |
Description
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.
.setStatus()
.setStatus*( status : integer )
Parameter | Type | Description | |
---|---|---|---|
status | integer | → | Status to set |
Description
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
Description
The .status
property contains the current status of the outgoing message. This property can be set using the setStatus()
function.