daraja restful
2.5
تمديد مريح لإطار Daraja HTTP
فيما يلي مثال قصير ، يقوم بتسجيل معالج طلب في PATH Hello الذي يتعامل مع HTTP GETS ، ولكن فقط إذا كان طلب 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);