Script event for MyChat Server: OnRequestByREST

Script event for MyChat Serve: OnRequestByREST

An event that occurs when the POST or GET query is sent to MyChat WEB Server via http/https (REST).

 

The address to which you need to send a query:

[protocol]://[server_address]:[port]/rest/[scriptname]/?[parameters]

 

A description of the query parameters:

Parameter

Value

protocol

http or https;

server_address

IP address or the domain name of MyChat Server;

port

port for MyChat WEB Server work. If it is 80/http or 443/https — you do not have to specify this parameter;

scriptname

script name in the OnRequestByREST event section that is executed by this query. The letter case is important, "myScript" and "myscript" are different scripts. Script MUST be enabled!

parameters

query parameters. For example, data=test&key=value#testhash

 

Event template

function OnRequestByREST(sBody, sParams, sHeaders, sURL, sIPv4, sIPv6: string; iType: integer): string;
begin
  // your own code

  result := '{}';
end;

begin

end.

You can use your own code instead of the comment.

 

A description of script event parameters

Parameter

Type

Value

sBody

string

query text;

sParams

string

query parameters;

sHeaders

string

query html headers;

sURL

string

query full URL;

sIPv4

string

client IP address in the IPv4 format;

sIPv6

string

client IP address in the IPv6 format;

iType

integer

query type. 0 — GET, 1 — POST.

 

Return value

Function must return JSON object as a text string. If you won't return anything then an empty object ("{}") will return automatically.This answer is given to the service that made POST or GET query to MyChat WEB Server.
 

Example

const
  LOG_FILE = 'c:\temp\log.txt';
  
function OnRequestByREST(sBody, sParams, sHeaders, sURL, sIPv4, sIPv6: string; iType: integer): string;
begin
    case iType of
      0: Protocol('GET request', LOG_FILE, true);
      1: Protocol('POST request', LOG_FILE, true);
    end;

  Protocol('sIPv4    : ' + sIPv4, LOG_FILE, true);
  Protocol('sIPv6    : ' + sIPv6, LOG_FILE, true);
  Protocol('sBody    : ' + sBody, LOG_FILE, true);
  Protocol('sParams  : ' + sParams, LOG_FILE, true);
  Protocol('sURL     : ' + sURL, LOG_FILE, true);
  Protocol('sHeaders : ' + sHeaders, LOG_FILE, true);

  result := '{"done" : "ok"}';
end;

begin

end.

Скрипт обрабатывает входящий REST запрос из браузера клиента и сохраняет данные в файл. В ответ отдаёт шаблонный текстовый ответ в формате JSON.

The script processes the incoming REST query from the client  browser and saves the data in the file. In return, the script gives a text answer template in the JSON format.

The example of a query string in a browser: http://192.168.10.109:8080/rest/telegram/?data=test&key=value#testhash

 

In our example, MyChat WEB Server works on 192.168.10.109 IP address, 8080 port, without traffic encryption. Query script called "telegram":
 

An example of MyChat script for OnRequestByREST event

 

The result of script work

[31.05.2017 18:12:26] GET request

[31.05.2017 18:12:26] sIPv4    : 192.168.10.105

[31.05.2017 18:12:26] sIPv6    : ::ffff:192.168.10.105

[31.05.2017 18:12:26] sBody    : ?data=test&key=value

[31.05.2017 18:12:26] sParams  : ?data=test&key=value

[31.05.2017 18:12:26] sURL     : /rest/telegram/123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/

[31.05.2017 18:12:26] sHeaders : Host

192.168.10.109:8080

Connection

keep-alive

Cache-Control

max-age=0

Upgrade-Insecure-Requests

1

User-Agent

Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36

Accept

text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

DNT

1

Accept-Encoding

gzip, deflate, sdch

Accept-Language

ru,en-US;q=0.8,en;q=0.6,uk;q=0.4

Cookie

io=X7qdbz1grVCXbK2TAAAA
 

See also

Working with JSON in scripts

Protocol