Saltar al contenido principal
Versión: 20 R8 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. Los parámetros dados directamente en la petición con el verbo GET son manejados por la propiedad .urlQuery, mientras que los parámetros pasados en el cuerpo de la solicitud están disponibles a través de funciones como .getBlob() o 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.

Historia
LanzamientoModificaciones
20 R8Clase añadida

Ejemplo

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
devuelve el cuerpo de la petición como un Blob
.getHeader( key : Text ) : Text
devuelve el valor del encabezado key
.getJSON() : Variant
devuelve el cuerpo de la solicitud como una resolución JSON
.getPicture() : Picture
devuelve el cuerpo de la petición como una imagen (en caso de que un cuerpo enviado como una imagen)
.getText() : Text
devuelve el cuerpo de la solicitud como un valor de texto
headers : Object
los encabezados actuales del mensaje entrante como pares llave/valor (cadenas)
url : Text
la URL de la petición sin la parte IP:port y como cadena
urlPath : Collection
la URL de la solicitud sin la parte IP:port y como una colección de cadenas
urlQuery : Object
los parámetros de la petición cuando se han dado en la URL como pares llave/valor
verb : Text
el verbo usado por la petición
nota

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

.getBlob()

.getBlob() : Blob

ParámetrosTipoDescripción
ResultadoBlob<-Body of the request as a Blob

Descripción

La función .getBlob() devuelve el cuerpo de la petición como un 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

ParámetrosTipoDescripción
keyText->Header property to get
ResultadoText<-Valor de la propiedad del encabezado

Descripción

La función .getHeader() devuelve el valor del encabezado key .

nota

The key parameter is not case sensitive.

Ejemplo

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

.getJSON()

.getJSON() : Variant

ParámetrosTipoDescripción
ResultadoVariant<-JSON resolution of the body of the request

Descripción

La función .getJSON() devuelve el cuerpo de la solicitud como una resolución JSON.

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

.getPicture()

.getPicture() : Picture

ParámetrosTipoDescripción
ResultadoPicture<-Body of the request as picture

Descripción

La función .getPicture() devuelve el cuerpo de la petición como una imagen (en caso de que un cuerpo enviado como una imagen).

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

nota

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

ParámetrosTipoDescripción
ResultadoText<-Body of the request as text

Descripción

La función .getText() devuelve el cuerpo de la solicitud como un valor de texto.

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

Descripción

La propiedad .headers contiene los encabezados actuales del mensaje entrante como pares llave/valor (cadenas).

La propiedad .headers es de sólo lectura.

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

.url

url : Text

Descripción

La propiedad .url contiene la URL de la petición sin la parte IP:port y como cadena.

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.

nota

La parte "host" de la petición (IP:port) es suministrada por el encabezado host.

.urlPath

urlPath : Collection

Descripción

La propiedad .urlPath contiene la URL de la solicitud sin la parte IP:port y como una colección de cadenas.

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

Descripción

La propiedad .urlQuery contiene los parámetros de la petición cuando se han dado en la URL como pares llave/valor.

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
nota

Los parámetros dados en el cuerpo de la petición utilizando los verbos POST o PUT son manejados a través de funciones dedicadas: getText(), getPicture(), getBlob(), getJSON().

.verb

verb : Text

Descripción

La propiedad .verb contiene el verbo usado por la petición.

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

The .verb property is read-only.