[Previous][Up] Reference for unit 'fpHTTPClient' (#fcl)

Using the TFPHTTPClient

The TFPHTTPClient class provides both simple class methods for quick requests and instance-based methods for more control over the HTTP client behavior. For basic operations, use the static SimpleGet, SimplePost, and SimpleFormPost methods. For advanced features like custom headers, authentication, or cookie management, create a TFPHTTPClient instance.

The HTTP client automatically handles redirects (up to a configurable limit), manages cookies, and supports both HTTP and HTTPS protocols. For HTTPS support, you must include either opensslsockets or gnutlssockets in your program's uses clause.

program HTTPClientBasic;

{$mode objfpc}{$H+}

uses
  SysUtils, Classes, fphttpclient, opensslsockets; // opensslsockets enables HTTPS

var
  Client: TFPHTTPClient;
  Response: String;
  ResponseStream: TStringStream;
begin
  // Simple GET request using class method
  Response := TFPHTTPClient.SimpleGet('https://httpbin.org/get');
  Writeln('Simple GET response:');
  Writeln(Response);

  // Instance-based approach for more control
  Client := TFPHTTPClient.Create(nil);
  ResponseStream := TStringStream.Create('');
  try
    // Configure client
    Client.AllowRedirect := True;
    Client.MaxRedirects := 5;

    // Add custom headers
    Client.AddHeader('User-Agent', 'MyApp/1.0');
    Client.AddHeader('Accept', 'application/json');

    // Make request to stream
    Client.Get('https://httpbin.org/headers', ResponseStream);

    Writeln('Response with custom headers:');
    Writeln(ResponseStream.DataString);

    Writeln('Response Status Code: ', Client.ResponseStatusCode);
    Writeln('Response Headers:');
    Writeln(Client.ResponseHeaders.Text);
  finally
    ResponseStream.Free;
    Client.Free;
  end;
end.

Documentation generated on: Jan 27 2026