TsgcWebSocketHTTPServerEvents › OnCommandGet

OnCommandGet Event

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

Syntax

public event TIdHTTPCommandEventHandler OnCommandGet;
// delegate void TIdHTTPCommandEventHandler(TIdContext AContext, TIdHTTPRequestInfo ARequestInfo, TIdHTTPResponseInfo AResponseInfo)

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


void OnCommandGet(TsgcWSConnection Connection, TsgcWSHTTPRequestInfo RequestInfo,
  ref TsgcWSHTTPResponseInfo ResponseInfo)
{
  if (RequestInfo.Document == "/")
  {
    ResponseInfo.ContentText = "<html><head><title>Test Page</title></head><body></body></html>";
    ResponseInfo.ContentType = "text/html";
    ResponseInfo.ResponseNo = 200;
  }
}

Back to Events