daraja restful
2.5
Расширение Restful для рамки Daraja http
Вот краткий пример, он регистрирует обработчик запросов по пути Hello, который обрабатывает HTTP GET запросов, но только в том случае, если HTTP -запрос также указывает, что клиент принимает ответы с текстом типа контента/HTML. (Ответ http ошибки будет возвращен, если клиент HTTP пытается отправить запрос POST или если клиент указывает другой тип контента).
&Path('hello');
&Produces('text/html');
GET
(procedure(Request: TdjRequest; Response: TdjResponse)
begin
Response.ContentText := '<html>Hello world!</html>';
end);
Структура поддерживает параметры пути, так что http: //mydomain.local/myapp/orders/123 будет маршрутизирован для этого параметризованного обработчика запроса:
&Path('orders/{orderId}')
&Produces('text/html');
GET
(procedure(Request: TdjRequest; Response: TdjResponse)
begin
Response.ContentText :=
Format('<html>Thank you for your order %s</html>',
[Request.Params.Values['orderId']]);
end);
Если ресурс имеет более одного представления (HTML, XML или JSON), с ним можно обработать с использованием одного и того же значения пути, но различный тип MIME создает атрибуты:
// respond to HTML browsers
&Path('myresource');
&Produces('text/html');
GET(procedure(Request: TdjRequest; Response: TdjResponse)
begin
Response.ContentText :=
'<html>Hello world!</html>';
end);
// respond to XML client
&Path('myresource');
&Produces('application/xml');
GET(procedure(Request: TdjRequest; Response: TdjResponse)
begin
Response.ContentText := '<xml>Hello world!</xml>';
Response.CharSet := 'utf-8';
end);
// respond to JSON client
&Path('myresource');
&Produces('application/json');
GET(procedure(Request: TdjRequest; Response: TdjResponse)
begin
Response.ContentText := '{"msg":"Hello world!"}';
Response.CharSet := 'utf-8';
end);