TsgcWebSocketHTTPServerEvents › OnCommandGet

OnCommandGet Event

Fires when the HTTP server receives a GET, POST, or HEAD request so the application can build the response.

Syntax

property OnCommandGet: TIdHTTPCommandEvent;
// TIdHTTPCommandEvent = procedure(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo) of object

Default Value

Remarks

OnCommandGet is the main entry point for HTTP traffic (GET, POST and HEAD) served by the component. ARequestInfo exposes the request (Document, Params, AuthUsername, Headers, PostStream, RemoteIP...) and AResponseInfo is the outgoing response: set ContentText or ContentStream with the body, ContentType with the MIME type, and ResponseNo with the HTTP status code (200, 404...); call AResponseInfo.ServeFile to dispatch a file from disk. Handlers run in the context of the connection thread so avoid direct UI access or switch to a synchronized dispatch. Requests for files that already live under DocumentRoot are served automatically and do not trigger this event.

Example


procedure OnCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo;
  AResponseInfo: TIdHTTPResponseInfo);
begin
  if ARequestInfo.Document = '/' then
  begin
    AResponseInfo.ContentText := '<html><head><title>Test Page</title></head><body></body></html>';
    AResponseInfo.ContentType := 'text/html';
    AResponseInfo.ResponseNo := 200;
  end;
end;

Back to Events