Skip to main content
Version: 21 R4 BETA

IncomingMessage

The 4D.IncomingMessage class allows you to handle the object received by a custom HTTP request handler. HTTP requests and their properties are automatically received as an instance of the 4D.IncomingMessage class. Parameters given directly in the request with GET verb are handled by the .urlQuery property, while parameters passed in the body of the request are available through functions such as .getBlob() or getText().

The HTTP request handler can return any value (or nothing). It usually returns an instance of the 4D.OutgoingMessage class.

All properties of this class are read-only. They are automatically filled by the request handler.

History
ReleaseChanges
20 R8Class added

Example​

The following HTTPHandlers.json file has been defined:

[
{
"class": "GeneralHandling",
"method": "gettingStarted",
"pattern": "start",
"verbs": "get, post"
}
]

The http://127.0.0.1/start/example?param=demo&name=4D request is run with a GET verb in a browser. It is handled by the gettingStarted function of the following GeneralHandling singleton class:

shared singleton Class constructor()

Function gettingStarted($request : 4D.IncomingMessage) : 4D.OutgoingMessage

var $result:=4D.OutgoingMessage.new()
var $body : Text

$body:="Called URL: "+$request.url+"\n"

$body+="The parameters are received as an object: \n"+JSON Stringify($request.urlQuery; *)+"\n"

$body+="The verb is: "+$request.verb+"\n"

$body+="There are "+String($request.urlPath.length)+" url parts - Url parts are: "\
+$request.urlPath.join(" - ")+"\n\n"


$result.setBody($body)
$result.setHeader("Content-Type"; "text/plain")

return $result

The request is received on the server as $request, an object instance of the 4D.IncomingMessage class.

Here is the response:

Called URL: /start/example? param=demo&name=4D
The parameters are received as an object:
{
"param": "demo",
"name": "4D"
}
The verb is: GET
There are 2 url parts - Url parts are: start - example

IncomingMessage Object​

4D.IncomingMessage objects provide the following properties and functions:

.getBlob() : Blob
returns the body of the request as a Blob
.getHeader( key : Text ) : Text
returns the value of the key header
.getJSON() : Variant
returns the body of the request as a JSON resolution
.getPicture() : Picture
returns the body of the request as a picture (in case of a body sent as a picture)
.getText() : Text
returns the body of the request as a text value
headers : Object
the current headers of the incoming message as key/value pairs (strings)
url : Text
the URL of the request without the IP:port part and as a string
urlPath : Collection
the URL of the request without the IP:port part and as a collection of strings
urlQuery : Object
the parameters of the request when they have been given in the URL as key/value pairs
verb : Text
the verb used by the request
note

A 4D.IncomingMessage object is a non-sharable object.

.getBlob()​

.getBlob() : Blob

ParameterTypeDescription
ResultBlob<-Body of the request as a Blob

Description​

The .getBlob() function returns the body of the request as a Blob.

If the body has not been given as a binary content, the function tries to convert the value but it can give unexpected results.

.getHeader()​

.getHeader( key : Text ) : Text

ParameterTypeDescription
keyText->Header property to get
ResultText<-Value of the header property

Description​

The .getHeader() function returns the value of the key header.

note

The key parameter is not case sensitive.

Example​

var $value : Text
var $request : 4D.IncomingMessage
$value := $request.getHeader("content-type")

.getJSON()​

.getJSON() : Variant

ParameterTypeDescription
ResultVariant<-JSON resolution of the body of the request

Description​

The .getJSON() function returns the body of the request as a JSON resolution.

If the body has not been given as JSON valid content, an error is raised.

.getPicture()​

.getPicture() : Picture

ParameterTypeDescription
ResultPicture<-Body of the request as picture

Description​

The .getPicture() function returns the body of the request as a picture (in case of a body sent as a picture).

The content-type must be given in the headers to indicate that the body is a picture.

note

If the request is built using the HTTPRequest class, the picture must be sent in the body as a Blob with the appropriate content-type.

If the body is not received as a valid picture, the function returns null.

.getText()​

.getText() : Text

ParameterTypeDescription
ResultText<-Body of the request as text

Description​

The .getText() function returns the body of the request as a text value.

If the body has not been given as a string value, the function tries to convert the value but it can give unexpected results.

.headers​

headers : Object

Description​

The .headers property contains the current headers of the incoming message as key/value pairs (strings).

The .headers property is read-only.

Header names (keys) are lowercased. Note header names are case sensitive.

.url​

url : Text

Description​

The .url property contains the URL of the request without the IP:port part and as a string.

For example, if the request is addressed to: "http://127.0.0.1:80/docs/invoices/today", the .url property is "/docs/invoices/today".

The .url property is read-only.

note

The "host" part of the request (IP:port) is provided by the host header.

.urlPath​

urlPath : Collection

Description​

The .urlPath property contains the URL of the request without the IP:port part and as a collection of strings.

For example, if the request is addressed to: "http://127.0.0.1:80/docs/invoices/today", the .urlPath property is ["docs", "invoices" ,"today"].

The .urlPath property is read-only.

.urlQuery​

urlQuery : Object

Description​

The .urlQuery property contains the parameters of the request when they have been given in the URL as key/value pairs.

The .urlQuery property is read-only.

Parameters can be passed in the URL of requests directly or as JSON contents.

Direct parameters​

Example: http://127.0.0.1:8044/myCall?firstname=Marie&id=2&isWoman=true

In this case, parameters are received as stringified values in the urlQuery property: urlQuery = {"firstname":"Marie" ,"id":"2" ,"isWoman":"true"}

JSON contents parameters​

Example: http://127.0.0.1:8044/myCall/?myparams='[{"firstname": "Marie","isWoman": true,"id": 3}]'.

Parameters are passed in JSON format and enclosed within a collection.

In this case, parameters are received as JSON text in the urlQuery property and can be parsed using JSON Parse.

//urlQuery.myparams: "[{"firstname": "Marie","isWoman": true,"id": 3}]"
$test:=Value type(JSON Parse($r.urlQuery.myparams))=Is collection) //true

Special characters such as simple quotes or carriage returns must be escaped.

Example: http://127.0.0.1:8044/syntax/?mdcode=%60%60%604d

//urlQuery.mdcode = ```4d
$test:=Length($r.urlQuery.mdcode) //5
note

Parameters given in the body of the request using POST or PUT verbs are handled through dedicated functions: getText(), getPicture(), getBlob(), getJSON().

.verb​

verb : Text

Description​

The .verb property contains the verb used by the request.

HTTP and HTTPS request verbs include for example "get", "post", "put", etc.

The .verb property is read-only.