Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Automatically Decode GZIP In TRESTResponse?


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 4 - Last Post: Oct 25, 2014 8:43 AM Last Post By: Eli M
Eli M

Posts: 1,346
Registered: 11/9/13
Automatically Decode GZIP In TRESTResponse?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 17, 2014 9:52 AM
It doesn't seem possible to assign a compressor or intercept to the TRESTClient.

If I set TRESTRequest.AcceptEncoding to 'gzip, deflate' I receive a gzip encoded response from a server that supports gzip.

However, in TIdHTTP I think it would automatically decode it. In TRESTResponse.Content it is still gzip encoded and I have to decode it manually with TIdCompressorZlib.DecompressGZipStream(). Is there a way for TRESTResponse to decode it automatically?
Hakan APAYDIN

Posts: 2
Registered: 1/19/12
Re: Automatically Decode GZIP In TRESTResponse?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 25, 2014 6:35 AM   in response to: Eli M in response to: Eli M
Eli M wrote:
It doesn't seem possible to assign a compressor or intercept to the TRESTClient.

If I set TRESTRequest.AcceptEncoding to 'gzip, deflate' I receive a gzip encoded response from a server that supports gzip.

However, in TIdHTTP I think it would automatically decode it. In TRESTResponse.Content it is still gzip encoded and I have to decode it manually with TIdCompressorZlib.DecompressGZipStream(). Is there a way for TRESTResponse to decode it automatically?

I want more information I have about the subject? Why does not automatically decode TRESTResponse ?
Eli M

Posts: 1,346
Registered: 11/9/13
Re: Automatically Decode GZIP In TRESTResponse?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 25, 2014 7:18 AM   in response to: Hakan APAYDIN in response to: Hakan APAYDIN
If you set RESTRequest.AcceptEncoding := 'gzip, deflate'; you'll get back gzipped content if the server supports it. In TIdHTTP it can be setup to automatically decode it. In TRESTResponse it seems like it has to be done manually with code like this:

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;
Hakan APAYDIN

Posts: 2
Registered: 1/19/12
Re: Automatically Decode GZIP In TRESTResponse?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 25, 2014 7:50 AM   in response to: Eli M in response to: Eli M
Eli M wrote:
If you set RESTRequest.AcceptEncoding := 'gzip, deflate'; you'll get back gzipped content if the server supports it. In TIdHTTP it can be setup to automatically decode it. In TRESTResponse it seems like it has to be done manually with code like this:

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;

Thans for the example.I get an error in the following code . How can I transfer information to returning again. I need to use the information returned restresponsedatasetadapter.

RESTResponse1.Content := DecodeGZIPContent(RESTResponse1.RawBytes)
Eli M

Posts: 1,346
Registered: 11/9/13
Re: Automatically Decode GZIP In TRESTResponse?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 25, 2014 8:43 AM   in response to: Hakan APAYDIN in response to: Hakan APAYDIN
You'd want to do something like

MyStringList := TStringList.Create;
MyStringList.Append(DecodeGZIPContent(RESTResponse1.RawBytes));

// do something with the content in MyStringList.Text

MyStringList.Free;

Maybe TRestResponseDatasetAdapter decodes it automatically (like I was looking for)?

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02