Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dealing with binaries and text in TIdHTTP
For context, I'm trying to create a simple abstraction over TIdHTP. So far I have something along these lines:

 THTTPRequest = record
   Method: (
   URL: string;
   ContentType: string;
   Body: string;

 THTTPResponse = record
   StatusCode: SmallInt;
   Body: string;

 THTTPClient = class
    function Perform(const ARequest: THTTPRequest): THTTPResponse; virtual; abstract;

 TIndyHTTPClient = class(THTTPClient)
   function Perform(const ARequest: THTTPRequest): THTTPResponse; override;

function TIndyHTTPClient.Perform(const ARequest: THTTPRequest): THTTPResponse;
 LClient: TIdHTTP;
 LRequestBodyStream: TStringStream;
 Result := Default(THTTPResponse);
 LRequestBodyStream := TStringStream.Create(ARequest.Body);
   LClient := TIdHTTP.Create(nil);
     LClient.HandleRedirects := True;
     LClient.HTTPOptions := LClient.HTTPOptions + [hoNoProtocolErrorException];
     LClient.Request.ContentType := ARequest.ContentType;
     case ARequest.Method of
         Result.Body := LClient.Get(ARequest.URL);
         Result.Body := LClient.Put(ARequest.URL, LRequestBodyStream);
         Result.Body := LClient.Post(ARequest.URL, LRequestBodyStream);
     Result.StatusCode := LClient.ResponseCode;

The issue is that not all requests and responses will be text based. I'd like to keep the interface as uniform as possible. Although filling the Body of a THTTPRequest with TBytes converted by StringOf did not result in any instantly noticeable problem, it does worry me that unwanted conversions may happen that corrupt the data. Conversely, I'm not sure if BytesOf on the Body of a THTTPResponse will give me the binary response (in case of files) exactly as it was sent. I could switch to always using TBytes for both, but then I lose all encoding/decoding intelligence that Indy has.

What is the cleanest solution (i.e. smallest number of fields/types) that will work?

Messages In This Thread
Dealing with binaries and text in TIdHTTP - by thijsvandien - 12-17-2018, 05:49 PM

Forum Jump:

Users browsing this thread: 1 Guest(s)