Skip to content

Client Overview

MARS ships a complete client library for consuming REST services from Delphi. It works against any REST server, with extra conveniences when the server is MARS (JSON ↔ record mapping, JWT handling, FireDAC dataset sync).

The client is a set of components you can drop on a form at design time or create in code. Their hierarchy mirrors the server:

TMARSClient (transport: Net / Http / Indy)
└── TMARSClientApplication        (AppName, default media types)
    ├── TMARSClientToken          (login / JWT)
    ├── TMARSClientResource        (raw)
    ├── TMARSClientResourceJSON    (records & objects ↔ JSON)
    ├── TMARSClientResourceStream  (binary)
    ├── TMARSClientResourceFormData / ...FormUrlEncoded
    ├── TMARSClientResourceSSE     (server-sent events)
    └── TMARSFDResource            (FireDAC datasets)

The URL of a call is composed the same way as on the server:

Client.MARSEngineURL  +  /Application.AppName  +  /Resource.Resource  +  /path params
http://localhost:8080/rest  /default            /helloworld

A first call in code

pascal
uses
  MARS.Client.Client.Net, MARS.Client.Application, MARS.Client.Resource;

var
  LClient: TMARSNetClient;
  LApp: TMARSClientApplication;
  LRes: TMARSClientResource;
begin
  LClient := TMARSNetClient.Create(nil);
  LApp := TMARSClientApplication.Create(nil);
  LRes := TMARSClientResource.Create(nil);
  try
    LClient.MARSEngineURL := 'http://localhost:8080/rest';

    LApp.Client := LClient;
    LApp.AppName := 'default';

    LRes.Application := LApp;
    LRes.Resource := 'helloworld';

    ShowMessage(LRes.GETAsString);   // -> "Hello World!"
  finally
    LRes.Free; LApp.Free; LClient.Free;
  end;
end;

The same wiring at design time is just dropping the three components and setting their properties in the Object Inspector — see Components.

Choosing a transport

Three interchangeable client transports are provided; they share the same API (MARSEngineURL, timeouts, auth, HTTP verbs):

ComponentUnitBacked byNotes
TMARSNetClientMARS.Client.Client.NetTNetHTTPClient (RTL)Default modern choice; cross-platform.
TMARSHttpClientMARS.Client.Client.HttpTHTTPClient (RTL)Fine-grained control (redirects, certificates, compression).
TMARSIndyClientMARS.Client.Client.IndyIndyFor environments that standardize on Indy.

All descend from TMARSCustomClient (MARS.Client.Client).

What's next

Released under the Apache License 2.0.