daraja restful
2.5
Daraja HTTP框架的静止扩展
这是一个简短的示例,它在路径Hello上注册一个请求处理程序,该请求处理程序处理HTTP获取请求,但前提是HTTP请求还指定客户端接受内容类型文本/HTML的响应。 (如果HTTP客户端试图提交发布请求,或者客户端指定其他内容类型,则将返回HTTP错误响应)。
&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);