Better Performance with REST Compression

Many Delphi applications, esp. mobile iOS or Android apps, are using REST, to retrieve data from a backend. Often TRESTClient and TRESTRequest are used to get access to an external REST api. There several Blogs and CodeRage videos (including from me), that demonstrate how to do this. Even the Delphi online documentation has a fairly simple tutorial on this topic:

Delphi Songsterr REST Tutorial

Most of these sample have on thing in common: They are slower as possible!

Most REST APIs are hosted on more or less modern WEb server, be it IIS, Apache, Nginx or whatever. All those support HTTP(S) compression. The actual api implementation usually doesn’t know about that.

Following the example from Delphi’s documentation, after executing the following request from a Webbrowser and investigating the transferred data in the browser’s Web console, you will probably think like „Excellent, that chatty XML has been compressed down to just about 10%“. And indeed this may result in an important speedup for your app.

https://www.songsterr.com/a/ra/songs.xml?pattern=Marley

In Firefox‘ Web Console you can easily identify the compression – obviously the XML data is shrunk down to less than 10%:

Komprimierte Daten

If you now do the same test with Delphi’s RESTDebugger (which internally uses TRESTClient), then the problem gets apparent immediately:

Clearly, data is transferred uncompressed here – slowly that is. The reason is, that an HTTP-Sever usually doesn’t compress data „just so“. The browser/client has to ask for it. Common Web browsers automatically ask the server to compress data. Delphi’s TRESTClient does not do this automatically.

Solution:

procedure TForm1.Button1Click(Sender: TObject);
var
  LValue:TJSONValue;
begin
  RESTRequest1.AcceptEncoding := 'gzip, deflate, br';
  RESTRequest1.Execute;
  LValue:=RESTResponse1.JSONValue;
  MemoContent.Text:= LValue.ToString;
end;

This asks the Web server to compress data, preferably using „gzip“. You can try that in Delphi’s RESTDebugger by adding a „Header Parameter“ like that:

and – surprise – the server sends back compressed data:

Setting Accept-Encoding via TRESTRequest.AcceptEncoding or via Header-Parameter, does not matter. Important though, check that “ Do not encode“ checkbox in RESTDebugger. Or set „DoNotEncode“ in your source code (without that, the spaces and commas will be URL encoded) :

  //Either like this
  LRequest.AcceptEncoding := 'gzip, deflate, br';
  //OR like this
  LParam := LRequest.Params.AddHeader('Accept-Encoding', 'br, gzip, deflate');
  LParam.Options := [poDoNotEncode];
  //-->> same result!

 

Wir benutzen Cookies um die Nutzerfreundlichkeit der Webseite zu verbessen. Durch Deinen Besuch stimmst Du dem zu.