Saltar para o conteúdo principal
Versão: 20 R9 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). Geralmente ele retorna uma instância da classe 4D.OutgoingMessage.

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

História
ReleaseMudanças
20 R8Classe adicionada

Exemplo

O seguinte arquivo HTTPHandlers.json foi definido:

[
{
"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

A solicitação é recebida no servidor como $request, uma instância de objeto da classe 4D.IncomingMessage.

Aqui está a resposta:

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

Objeto IncomingMessage

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

.getBlob() : Blob
retorna o corpo da solicitação como um Blob
.getHeader( key : Text ) : Text
retorna o valor do cabeçalho key
.getJSON() : Variant
retorna o corpo da solicitação como uma resolução JSON
.getPicture() : Picture
returns the body of the request as a picture (in case of a body sent as a picture)
.getText() : Text
retorna o corpo da solicitação como valor de texto
headers : Object
os cabeçalhos atuais da mensagem recebida como pares chave/valor (strings)
url : Text
a URL da solicitação sem a parte IP:port e como um string
urlPath : Collection
the URL of the request without the IP:port part and as a collection of strings
urlQuery : Object
os parâmetros da solicitação quando eles são fornecidos no URL como pares de chave/valor
verb : Text
o verbo usado pelo pedido
nota

Um objeto 4D.IncomingMessage é um objeto não compartilhável.

.getBlob()

.getBlob() : Blob

ParâmetroTipoDescrição
ResultadosBlob<-Body of the request as a Blob

Descrição

A função .getBlob() retorna o corpo da solicitação como um 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âmetroTipoDescrição
|Text->Propriedade de cabeçalho a obter
ResultadosText<-Valor da propriedade do cabeçalho

Descrição

A função .getHeader() retorna o valor do cabeçalho key.

nota

O parâmetro key não diferencia maiúsculas de minúsculas.

Exemplo

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

.getJSON()

.getJSON() : Variant

ParâmetroTipoDescrição
ResultadosDiferente de<-JSON resolution of the body of the request

Descrição

A função .getJSON() retorna o corpo da solicitação como uma resolução JSON.

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

.getPicture()

.getPicture() : Picture

ParâmetroTipoDescrição
ResultadosImagem<-Body of the request as picture

Descrição

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.

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âmetroTipoDescrição
ResultadosText<-Corpo da solicitação como texto

Descrição

A função .getText() retorna o corpo da solicitação como 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

Descrição

A propriedade .headers contém os cabeçalhos atuais da mensagem recebida como pares chave/valor (strings).

A propriedade .headers é somente leitura.

Nomes de cabeçalho (chaves) são menores. Observe que os nomes dos cabeçalhos diferenciam maiúsculas de minúsculas.

.url

url : Text

Descrição

A propriedade .url contém a URL da solicitação sem a parte IP:port e como um 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".

A propriedade .url é somente leitura.

nota

A parte "host" do pedido (IP:port) é fornecida pelo cabeçalho host.

.urlPath

urlPath : Collection

Descrição

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"].

A propriedade .urlPath é somente leitura.

.urlQuery

urlQuery : Object

Descrição

A propriedade .urlQuery contém os parâmetros da solicitação quando eles são fornecidos no URL como pares de chave/valor.

A propriedade .urlQuery é somente leitura.

Os parâmetros podem ser passados no URL das solicitações diretamente ou como conteúdo JSON.

Direct parameters

Exemplo: http://127.0.0.1:8044/minhaCall?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"}

Parâmetros conteúdo JSON

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.

Nesse caso, os parâmetros são recebidos como texto JSON na propriedade urlQuery e podem ser analisados usando 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.

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

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

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

Descrição

A propriedade .verb contém o verbo usado pelo pedido.

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

A propriedade .verb é somente leitura.