← API reference
Daraja HTTP Framework · for Delphi and Free Pascal

How a web request reaches your Pascal code

When someone opens a URL served by your Daraja program, the request passes through a few building blocks before your code runs, and the answer travels back the same way. The left column shows what each block does for you. The right column shows the line of Pascal that sets it up.

Daraja does this for you You write this Request going in Answer coming back
What happens
Your Pascal code

Someone opens a URL

A browser, a script or another program asks for http://127.0.0.1/tutorial/hello.

curl http://127.0.0.1/tutorial/hello
Try it from the command line, or open the address in a browser.

Listens for requests

TdjHTTPServerTdjHTTPConnector

Waits on a network port, receives each request and reads it. This part is built on Indy, the networking library that Daraja uses under the hood.

Server := TdjServer.Create(80);
80 is the port. Use another number, such as 8080, if 80 is taken.

Finds the right app

TdjServer

One server can host several apps side by side. The first part of the path, /tutorial, decides which app gets the request.

No app for that path? The visitor gets “404 Not Found”.
Server.Add(Context);
Server.Start;

An app, called a context

TdjWebAppContext

Groups everything that lives under one path, here /tutorial: which URL goes to which piece of your code, and settings the app shares, such as visitor sessions.

Context := TdjWebAppContext.Create('tutorial');

Picks your class for the URL

TdjWebComponentHandler

Looks at the rest of the path, /hello, and finds the class you registered for it. Patterns can be an exact path, a whole folder such as /api/*, or a file type such as *.html.

Context.Add(THelloWorldResource, '/hello');
This one line connects the URL to your class.

Filters: extra steps before and after

optionalTdjWebFilter

A filter wraps around your code, so it can do something first (check a login, write a log line) and something afterwards (change the page it produced). Filters apply to many URLs at once.

Context.Add(TResponseHtmlFilter, '*.html');

procedure TResponseHtmlFilter.DoFilter;
begin
  // before: runs first
  Chain.DoFilter(Context, Request, Response);
  // after: change the answer here
end;

Your code runs

your TdjWebComponent subclass

A request always has a type, called its method. Opening a page is GET; submitting a form is usually POST. Daraja calls the matching procedure, OnGet or OnPost, and you fill in the answer.

You only write the procedures you need. Requests of any other type get a standard “not allowed” answer (405).
type
  THelloWorldResource = class(TdjWebComponent)
  public
    procedure OnGet(Request: TdjRequest;
      Response: TdjResponse); override;
  end;

procedure THelloWorldResource.OnGet(...);
begin
  Response.ContentText := 'Hello, World!';
  Response.ContentType := 'text/plain';
end;

The answer goes back the same way

Whatever you put into Response travels back through any filters and is sent to the visitor. You never deal with the network yourself.

HTTP/1.1 200 OK
Content-Type: text/plain

Hello, World!

A few words you will meet

Request and response
The question a visitor sends (“give me /hello”) and the answer your program sends back.
Context
One app inside your server, reached through its own path, such as /tutorial.
Web component
Your class that produces the answer for one or more URLs.
Filter
Code that runs before and after your web components, for many URLs at once.
Status code
A number that tells the visitor how it went: 200 OK, 404 not found, 405 not allowed, 500 something failed on the server.
Method
The type of request. GET reads something, POST sends data, and there are a few more.