While writing a Delphi Rich client for a personal project talking to RESTful service I recently became frustrated with the available easy REST client interfaces for Delphi.

As a result, I wrote a small wrapper around the Indy TIdHttp component to provide a fluent interface upon which you can use to write REST clients. It’s lightweight wrapper, which currently only has the functionality that I required for this project but I decided to make it available in case anyone else wanted to use it.

You can view a simple example were we post a fictional example of PUT’ing a todo item below:

program SimpleRestRequestSample;


{$R *.res}

  RestRequest in 'RestRequest.pas';

var RestReq: TRestRequest;
    RestResp: THttpResponse;
    putParams: TStringList;
      putParams := TStringList.Create();
      putParams.Add('title=Buy milk');
      putParams.Add('due-date=01/01/2013 00:00:00');
      RestReq := TRestRequest.Create().Domain('localhost').Path('todo').WithCredentials('test', 'test');
      RestResp := RestReq.Put(putParams);
      if RestResp.ResponseCode = 200 then WriteLn('Your todo was added!');
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);

As you can see, the interface is designed to be fluent to make it easy to see the components of the request in your code.

The TRestResponse record that is returned contains the ResponseCode and the ResponseBody but does nothing with the returned body as the type of response will be determined by your Client and Server. For example, in my own client project, I set the Accept header to prefer ‘application/json’ and then parse this in the response after receiving a 200 OK response from the server with the application/json header,. This looks something like the below:

      request := TRestRequest.Create.Domain(Self.FAPIEndPoint).WithCredentials(Self.FUsername, Self.FPassword).Path('todo').Path(IntToStr(id));
      response := request.Get;
      if response.ResponseCode = 200 then
        result := jsonToObj(response.ResponseStr);
      end else Self.doOnErrorStatus(response.ResponseCode, response.ResponseStr);

There are a few limitations: for example, it doesn’t currently support the PATCH method as TIdHttp doesn’t currently support this method and I didn’t have the time or motivation to mock out TIdHttp so the unit test coverage doesn’t currently extend that far. Aside from that, I’ve found it pretty nice to work with in my own project so any thoughts would be appreciated.

I’m completely open to help or contributions and you can view the SimpleRestClient project on Github.

Tags: ,


  1. Pol on the 7th January 2013 remarked #

    Nice idea but wrapper sekund WinHttp would be better.

  2. Fabricio Colombo on the 7th January 2013 remarked #

    Some time ago I was searching for a Rest Client for Delphi also, but i’m not found, and i began to develop a project.
    Take a look: https://github.com/fabriciocolombo/delphi-rest-client-api

  3. ObjectMethodology.com on the 7th January 2013 remarked #

    Cool, some nice code to make things easier. You can’t beat that!

  4. jamiei on the 7th January 2013 remarked #

    @Pos: Interesting option.

    @Fabricio Colombo: Ooh, interesting, it’s rather more fully featured!

    @ObjectMethodology.com: Thanks!

  5. Phil on the 19th February 2013 remarked #

    You may also keep some ideas from the REST client part of MORMot :

  6. jamiei on the 24th February 2013 remarked #

    @Phil: I haven’t seen the MORMot framework yet but it looks like Fabricio’s framework.

  7. Dall'Ara Luca on the 22nd April 2013 remarked #

    Is it possibile upload zip file in the the post parameters?

    function Param(aKey: string; aValue: string): TRestRequest;

    Param function accepts string values for the Keys and not object

    Thanks in advance

  8. jamiei on the 22nd April 2013 remarked #

    @Dall’Ara Luca: It presently only supports “application/x-www-form-urlencoded” encoded params, the presence of a file would mean switching automatically to “multipart/form-data” encoding. Certainly do-able, but will require modifications. I’ve opened an issue on the github project at: https://github.com/jamiei/SimpleRestClient/issues/1

    I’ll use the issue for tracking.

  9. Dall'Ara Luca on the 24th April 2013 remarked #

    Thank you very much!

  10. jamiei on the 24th May 2013 remarked #

    @Dall’Ara Luca: Should be good to go. Check that issue.

  11. Guto on the 23rd March 2016 remarked #

    Is it possible to use Magento Rest API with your wrapper?

Leave a Comment