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).
History
Release | Changes |
---|---|
20 R7 | Class added |
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:
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 |
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 : Text ; value : Text )
Parameter | Type | Description | |
---|---|---|---|
key | Text | -> | Header property to set |
value | Text | -> | 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. For a list of "Content-Type" header values, please refer to the WEB SEND BLOB
documentation.
.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.