TsgcWebSocketServer_HTTPAPIEventos › OnHTTPRequest

OnHTTPRequest Evento

Se activa cuando el servidor recibe una solicitud HTTP para que la aplicación pueda construir la respuesta.

Sintaxis

property OnHTTPRequest: TsgcWSHTTPAPIRequestEvent;
// TsgcWSHTTPAPIRequestEvent = procedure(aConnection: TsgcWSConnection_HTTPAPI; const aRequestInfo: THttpServerRequest; var aResponseInfo: THttpServerResponse) of object

Valor Predeterminado

Observaciones

OnHTTPRequest es el punto de entrada principal para cada solicitud HTTP que HTTP.SYS entrega al servidor (GET, POST, PUT, DELETE, HEAD...). aRequestInfo expone la solicitud decodificada (Document, Method, Headers, ContentType, Content, QueryParams, Cookies, ContentLength, AuthExists/AuthUsername/AuthPassword, Stream) y aResponseInfo recoge la respuesta que la aplicación desea enviar: establezca ResponseNo, ContentText (o ContentStream / FileName para respuestas binarias/de archivo), ContentType, CustomHeaders, Date/Expires/LastModified y CacheControl según sea necesario. Deje aResponseInfo sin modificar para devolver el HTTP 200 predeterminado, o asigne ResponseNo a 404/500/... para indicar condiciones de error. Este evento no se activa para solicitudes de actualización WebSocket; estas se enrutan a través de OnConnect / OnHandshake en su lugar.

Ejemplo


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;

Volver a Eventos