daraja restful
2.5
Daraja HTTPフレームワークのRESTFUL EXTRENTION
簡単な例は、HTTP Get Requestsを処理するPath Helloでリクエストハンドラーを登録しますが、HTTPリクエストがクライアントがコンテンツタイプテキスト/HTMLで応答を受け入れることも指定した場合のみです。 (HTTPクライアントがPOSTリクエストを送信しようとする場合、またはクライアントが異なるコンテンツタイプを指定しようとする場合、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);