Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dealing with binaries and text in TIdHTTP
#4
It is clear that you don't understand how HTTP operates, or how TIdHTTP implements HTTP.

An HTTP message's body is just raw bytes. Various headers, including Content-Type, specify what the type, formatting, and transmission method of the body actually is.

TIdHTTP handles those details for you. However, you are responsible for choosing the appropriate overload of its Get()/Post()/Put() methods for not only the type of data you want to send, but also for the type of data you want to receive. TIdHTTP can send a request body from a TStrings or TStream, and can return a response body as a String or a TStream. There are many overloads to choose from:

Code:
procedure Get(AURL: string; AResponseContent: TStream); overload;
procedure Get(AURL: string; AResponseContent: TStream; AIgnoreReplies: array of Int16); overload;
function Get(AURL: string
  {$IFDEF STRING_IS_ANSI}; ADestEncoding: IIdTextEncoding = nil{$ENDIF}
): string; overload;
function Get(AURL: string; AIgnoreReplies: array of Int16
  {$IFDEF STRING_IS_ANSI}; ADestEncoding: IIdTextEncoding = nil{$ENDIF}
): string; overload;

function Post(AURL: string; const ASourceFile: String
  {$IFDEF STRING_IS_ANSI}; ADestEncoding: IIdTextEncoding = nil{$ENDIF}
): string; overload;
function Post(AURL: string; ASource: TStrings; AByteEncoding: IIdTextEncoding = nil
  {$IFDEF STRING_IS_ANSI}; ASrcEncoding: IIdTextEncoding = nil; ADestEncoding: IIdTextEncoding = nil{$ENDIF}): string; overload;
function Post(AURL: string; ASource: TStream
  {$IFDEF STRING_IS_ANSI}; ADestEncoding: IIdTextEncoding = nil{$ENDIF}
): string; overload;
function Post(AURL: string; ASource: TIdMultiPartFormDataStream
  {$IFDEF STRING_IS_ANSI}; ADestEncoding: IIdTextEncoding = nil{$ENDIF}
): string; overload;

procedure Post(AURL: string; const ASourceFile: String; AResponseContent: TStream); overload;
procedure Post(AURL: string; ASource: TStrings; AResponseContent: TStream; AByteEncoding: IIdTextEncoding = nil
  {$IFDEF STRING_IS_ANSI}; ASrcEncoding: IIdTextEncoding = nil{$ENDIF}); overload;
procedure Post(AURL: string; ASource, AResponseContent: TStream); overload;
procedure Post(AURL: string; ASource: TIdMultiPartFormDataStream; AResponseContent: TStream); overload;

function Put(AURL: string; ASource: TStream
  {$IFDEF STRING_IS_ANSI}; ADestEncoding: IIdTextEncoding = nil{$ENDIF}
): string; overload;
procedure Put(AURL: string; ASource, AResponseContent: TStream); overload;

In your THTTPRequest, using TBytes for the Body will work, but not very efficiently, especially when you start sending files, as TBytes requires its bytes to reside in memory. You don't want to load large files into memory. Not to mention, the extra overhead of requiring the user to convert their data to TBytes, and then you having to convert that to a format TIdHTTP will accept, such as a TStream (via TBytesStream, for instance), and vice versa for responses.

I would suggest using TStream for your Body data, then you can use TStringStream for text, TFileStream for files, TMemoryStream for blocks of memory, etc. You gain a lot more flexibility that way.

Also, when it comes to converting between bytes and strings, don't use BytesOf() and StringOf(), they don't offer you any control over the encoding used. Use TEncoding.GetBytes() and TEncoding.GetString() instead, or Indy's ToBytes() and BytesToString() functions (which offer encoding parameters) or use its IIdTextEncoding interface directly.

Reply


Messages In This Thread
RE: Dealing with binaries and text in TIdHTTP - by rlebeau - 12-17-2018, 09:08 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)