daraja restful
2.5
Daraja HTTP 프레임 워크에 대한 편안한 확장
다음은 짧은 예입니다. HTTP get requests를 처리하는 Path Hello에 요청 핸들러를 등록하지만 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.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);