If 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.