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.
História
Release | Mudanças |
---|---|
20 R8 | Classe 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
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
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 |
Um objeto 4D.IncomingMessage é um objeto não compartilhável.
.getBlob()
.getBlob() : Blob
Parâmetro | Tipo | Descrição | |
---|---|---|---|
Resultados | Blob | <- | 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âmetro | Tipo | Descrição | |
---|---|---|---|
| | Text | -> | Header property to get |
Resultados | Text | <- | Valor da propriedade do cabeçalho |
Descrição
A função .getHeader()
retorna o valor do cabeçalho key.
The key parameter is not case sensitive.
Exemplo
var $value : Text
var $request : 4D.IncomingMessage
$value := $request.getHeader("content-type")
.getJSON()
.getJSON() : Variant
Parâmetro | Tipo | Descrição | |
---|---|---|---|
Resultados | Diferente 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âmetro | Tipo | Descrição | |
---|---|---|---|
Resultados | Imagem | <- | 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.
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âmetro | Tipo | Descrição | |
---|---|---|---|
Resultados | Text | <- | Body of the request as text |
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. Note header names are case sensitive.
.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".
The .url
property is read-only.
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"].
The .urlPath
property is read-only.
.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.
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.
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.
Example: http://127.0.0.1:8044/syntax/?mdcode=%60%60%604d
//urlQuery.mdcode = ```4d
$test:=Length($r.urlQuery.mdcode) //5
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.
The .verb
property is read-only.