Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works
Anything · Lazarus or Delphi
Random projectRandom project

daraja-restful

RESTful extension for Daraja HTTP Framework

View on GitHub

Member rating

No ratings yet.

Sign in to rate this project.

README

daraja-restful

RESTful extension for the Daraja HTTP Framework

How does it work?

Here is a short example, it registers a request handler at path hello which handles HTTP GET requests, but only if the HTTP request also specifies that the client accepts responses with content type text/html. (A HTTP error response will be returned if the HTTP client tries to submit a POST request, or if the client specifies a different content type).

pascal
&Path('hello');
&Produces('text/html');
GET
 (procedure(Request: TdjRequest; Response: TdjResponse)
  begin
    Response.ContentText := '<html>Hello world!</html>';
  end);

Path Parameters

The framework supports path parameters, so that http://mydomain.local/myapp/orders/123 will be routed to this parametrized request handler:

pascal
&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);

Multiple Resource Representations

If a resource has more than one representation (HTML, XML or JSON), this can be handled using the same Path value but different MIME type Produces attributes:

pascal
// respond to HTML browsers
&Path('myresource');
&Produces('text/html');
GET(procedure(Request: TdjRequest; Response: TdjResponse)
    begin
      Response.ContentText :=
        '<html>Hello world!</html>';
    end);
pascal
// 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);
pascal
// 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);