Decode GZIP Content From The REST Client In Delphi XE7 Firemonkey On Android And IOS
November 7, 2014
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
I’m trying to run this code in IOS, but it doesnt work.
In XE7 running on win32 works fine, but on IOS i get a exception: Error on call to ZLib library function inflateInit2_.
Hi,
I’m trying to run this code in IOS, but it doesnt work.
In XE7 running on win32 works fine, but on IOS i get a exception: Error on call to ZLib library function inflateInit2_.
Can you help me?