Get access to over 100 FireMonkey cross platform samples for Android, IOS, OSX, Windows, and Linux!

AndroidAppmethodCode SnippetDelphiFiremonkeyIOSOSXWindows

Decode GZIP Content From The REST Client In Delphi XE7 Firemonkey On Android And IOS

| Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOS

Delphi XE7 Firemonkey REST Client Decode GZIP Content | Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOSIf you’re building any kind of mobile app with the TRESTClient that ships with Delphi XE7 Firemonkey you’ll want it to use a minimum amount of data. One place where you can really reduce the amount of data usage is to compress the data between your REST server and your REST client app. The TRESTClient component uses TIdHTTP (and the HTTP protocol) which supports GZIP for compression. By default compression is not enabled in TRESTClient. You can enable compression by setting the TRESTRequest property AcceptEncoding to ‘gzip, deflate’. However, once the server starts sending you GZIPed content you will have to decode it. If the server supports GZIP and it gzips the contents the TRESTResponse.ContentEncoding property will be ‘gzip’. You will need to decode the content yourself at this point. For you to decode the content you can use the TRESTResponse.RawBytes property to access the data for decoding. Load the contents of RawBytes into a TMemoryStream and then use TIdCompressorZlib.DecompressGZipStream() to decompress the data. I’ve included a code snippet below. It should work on Android, IOS, OSX, and Windows plus it should compile for Appmethod as well.
if RESTResponse.ContentEncoding='gzip' then
DecodeGZIPContent(RESTResponse.RawBytes) // decode and do something with the content
else
RESTResponse.Content; // do something with the content

function DecodeGZIPContent(RawBytes: System.TArray<System.Byte>): String;
var
MSI: TMemoryStream;
MSO: TStringStream;
begin
MSI := TMemoryStream.Create;
MSO := TStringStream.Create;
MSI.WriteData(RawBytes,Length(RawBytes));
MSI.Seek(0,0);
// Zlib is a TIdCompressorZlib
Zlib.DecompressGZipStream(MSI,MSO);
MSI.DisposeOf;
MSO.Seek(0,0);
Result := MSO.DataString;
MSO.Free;
end;

Head over to the Embarcadero docwiki and check out all of the REST components available in the REST.Client unit for Delphi XE7 Firemonkey.

Have Delphi Firemonkey questions? Ask and get answers on StackOverflow.

Related posts
DelphiDemoFiremonkeyLinuxOSXShowcaseWindows

AutoBlogAI: FireMonkey Client To Leverage LLMs And Generative AI For Blogging

DelphiFiremonkeyShowcaseUtilityWindows

Unleashing Creativity With Song Writer AI: A Deep Dive

DelphiFiremonkeyShowcaseWindows

How To Build Stable Diffusion Text To Image Prompts

AndroidC++BuilderDelphiFiremonkeyIOSOSXWindows

FireMonkey 10.4.2 Features Updated iOS 14, Android 11, And macOS 11 Support Plus Hundreds Of Fixes

Sign up for our Newsletter and
stay informed

3 Comments

Leave a Reply