RESTRequest4Delphi Documentation

repository·master·Indexed 20 days ago

https://github.com/viniciussanchez/restrequest4delphi

A minimalist API for consuming REST services in Delphi and Lazarus. It features a fluent interface for HTTP requests (GET, POST, PUT, DELETE) and supports multiple underlying engines including Indy, Synapse, NetHTTP, and native Delphi/Lazarus clients. The library allows for extensibility via adapters and provides built-in support for Basic, Token, and TokenBearer authentication.

Tokens
1.4K
Snippets
5
Records
8
Agent score
20%

What's inside RESTRequest4Delphi

  1. Compare HTTP Engine performance and compliance

    master

    The project supports multiple HTTP engines for Delphi, allowing developers to choose between different implementations based on performance and platform requirements. A benchmark report compares the following engines against the https://httpbin.org API:

    • Delphi Client (Nativo): Generally the fastest for standard methods like GET, POST, and PUT. It utilizes native Windows APIs (WinInet) and benefits from OS-level connection pooling.
    • NetHTTPClient: Highly competitive, often winning in specific methods like DELETE and PATCH. It utilizes Windows APIs (WinHTTP) and benefits from OS-level connection pooling.
    • Indy Engine: Provides high robustness and granular socket control. It is ideal for legacy multi-platform systems that require implementations independent of the operating system's system libraries, though it may exhibit higher latency in short iterations due to the lack of aggressive native Windows socket pooling without explicit keep-alive.

    All supported engines have demonstrated 100% compliance (success rate) when interacting with real-world web APIs.

  2. Use Adapters to extend functionality

    master

    Adapters allow you to extend RESTRequest4Delphi without modifying the core library. Common community adapters include:

    • dataset-serialize-adapter-restrequest4delphi: Loads a DataSet using the DataSet-Serialize library during a request.
    • csv-adapter-restrequest4delphi: Generates text or CSV files from request content.
  3. Perform HTTP requests (GET, POST, PUT, DELETE)

    master

    To use RESTRequest4D, include RESTRequest4D in your uses clause. The library uses a fluent interface starting with TRequest.New.

    GET Request

    var
      LResponse: IResponse;
    begin
      LResponse := TRequest.New.BaseURL('http://localhost:8888/users')
        .AddHeader('HeaderName', 'HeaderValue')
        .AddParam('ParameterName', 'ParameterValue')
        .Accept('application/json')
        .Get;
      if LResponse.StatusCode = 200 then
        ShowMessage(LResponse.Content);
    end;

    POST Request

    begin
      TRequest.New.BaseURL('http://localhost:8888/users')
        .ContentType('application/json')
        .AddBody('{"name":"Vinicius","lastName":"Sanchez","email":"viniciuss.sanchez@gmail.com"}')
        .Post;
    end;

    PUT Request

    begin
      TRequest.New.BaseURL('http://localhost:8888/users/1')
        .ContentType('application/json')
        .AddBody('{"name":"Vinicius","lastName":"Scandelai Sanchez","email":"viniciuss.sanchez@gmail.com"}')
        .Put;
    end;

    DELETE Request

    begin
      TRequest.New.BaseURL('http://localhost:8888/users/1')
        .Accept('application/json')
        .Delete;
    end;
    uses RESTRequest4D;
  4. Install RESTRequest4Delphi

    master

    You can install RESTRequest4Delphi using either manual installation or the Boss dependency manager.

    Manual Installation

    Add the src folder from the repository to your project's search path: Project > Options > Building > Delphi Compiler > Search path

    Using Boss

    Run the following command in your terminal:

    boss install github.com/viniciussanchez/RESTRequest4Delphi
  5. Configure HTTP Request Engines

    master

    RESTRequest4Delphi supports multiple HTTP engines. The default engine depends on your IDE:

    • Delphi: Uses TRESTRequest, TRESTResponse, and TRESTClient.
    • Lazarus: Uses fphttpclient.

    You can switch the engine by adding specific compiler directives to Project > Options > Delphi Compiler > Conditional defines:

    DirectiveEngine
    RR4D_INDYIndy
    RR4D_SYNAPSESynapse
    RR4D_ICSICS Overbyte
    RR4D_NETHTTPNetHTTP

    Note for Lazarus users: To switch from the default fphttpclient, use RR4D_INDY or RR4D_SYNAPSE.

  6. Get data as a DataSet using Adapters

    master

    You can use adapters to automatically map request responses to a DataSet (e.g., an FDMemTable).

    begin
      TRequest.New.BaseURL('http://localhost:8888/users')
        .Adapters(TDataSetSerializeAdapter.New(FDMemTable))
        .Accept('application/json')
        .Get;
    end;
    begin
      TRequest.New.BaseURL('http://localhost:8888/users')
        .Adapters(TDataSetSerializeAdapter.New(FDMemTable))
        .Accept('application/json')
        .Get;
    end;
  7. Configure Authentication

    master

    You can set credentials for all subsequent requests using BasicAuthentication, Token, or TokenBearer. This is typically done once before making requests.

    begin
      Request.BasicAuthentication('username', 'password');
      Request.Token('token-type ' + token);
      Request.TokenBearer(token);
    end;
    begin
      Request.BasicAuthentication('username', 'password');
      Request.Token('token-type ' + token);
      Request.TokenBearer(token);
    end;
  8. Reference: HTTP Engine Performance Benchmarks

    master

    The following table summarizes the average latency (in ms) observed during benchmarking for different HTTP methods across the available engines. Lower values indicate better performance.

    | Método HTTP | Delphi Client (Nativo) | NetHTTPClient | Indy Engine | Vencedor (Mais Rápida) |
    | :--- | ::---: | :---: | :---: | :---: |
    | **GET** | 147 ms | 170 ms | 629 ms | **Nativo** 🚀 |
    | **POST** | 148 ms | 180 ms | 749 ms | **Nativo** 🚀 |
    | **PUT** | 166 ms | 167 ms | 879 ms | **Nativo** 🚀 |
    | **DELETE** | 170 ms | 140 ms | 609 ms | **NetHTTP** 🚀 |
    | **PATCH** | 4.140 ms | 383 ms | 811 ms | **NetHTTP** 🚀 |