TsgcWebSocketServer_HTTPAPI › Events › OnHTTPRequest
Fires when the server receives an HTTP request so the application can build the response.
property OnHTTPRequest: TsgcWSHTTPAPIRequestEvent;
// TsgcWSHTTPAPIRequestEvent = procedure(aConnection: TsgcWSConnection_HTTPAPI; const aRequestInfo: THttpServerRequest; var aResponseInfo: THttpServerResponse) of object
—
OnHTTPRequest is the main entry point for every HTTP request that HTTP.SYS delivers to the server (GET, POST, PUT, DELETE, HEAD...). aRequestInfo exposes the decoded request (Document, Method, Headers, ContentType, Content, QueryParams, Cookies, ContentLength, AuthExists/AuthUsername/AuthPassword, Stream) and aResponseInfo collects the response the application wants to send back: set ResponseNo, ContentText (or ContentStream / FileName for binary/file responses), ContentType, CustomHeaders, Date/Expires/LastModified and CacheControl as needed. Leave aResponseInfo untouched to return the default HTTP 200, or assign ResponseNo to 404/500/... to signal error conditions. This event does not fire for WebSocket upgrade requests; those are routed through OnConnect / OnHandshake instead.
procedure OnHTTPRequest(aConnection: TsgcWSConnection_HTTPAPI;
const aRequestInfo: THttpServerRequest;
var aResponseInfo: THttpServerResponse);
begin
if aRequestInfo.Method = 'GET' then
begin
if aRequestInfo.Document = '/test.html' then
begin
aResponseInfo.ResponseNo := 200;
aResponseInfo.ContentText := 'OK';
aResponseInfo.ContentType := 'text/html; charset=UTF-8';
end
else
aResponseInfo.ResponseNo := 404;
end
else
aResponseInfo.ResponseNo := 500;
end;