Perpanjangan Restful untuk Kerangka Daraja HTTP
Berikut adalah contoh singkat, ini mendaftarkan penangan permintaan di Path Hello yang menangani permintaan http mendapatkan, tetapi hanya jika permintaan HTTP juga menentukan bahwa klien menerima tanggapan dengan tipe konten Text/HTML. (Respons kesalahan HTTP akan dikembalikan jika klien HTTP mencoba mengirimkan permintaan posting, atau jika klien menentukan jenis konten yang berbeda).
&Path('hello');
&Produces('text/html');
GET
(procedure(Request: TdjRequest; Response: TdjResponse)
begin
Response.ContentText := '<html>Hello world!</html>';
end);
Kerangka kerja mendukung parameter jalur, sehingga http: //mydomain.local/myapp/orders/123 akan dialihkan ke pawang permintaan parametrized ini:
&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);
Jika sumber daya memiliki lebih dari satu representasi (HTML, XML atau JSON), ini dapat ditangani menggunakan nilai jalur yang sama tetapi tipe MIME yang berbeda menghasilkan atribut:
// 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);