Skip to main content

Overview

THorseResponse represents an HTTP response and provides methods to send content, set status codes, manage headers, and handle file downloads.
type
  THorseResponse = class
  private
    FWebResponse: TWebResponse; // TResponse in FPC
    FContent: TObject;
  public
    // Methods and properties
  end;

Response Methods

Send (String Content)

Send a string response to the client.
function Send(const AContent: string): THorseResponse; overload; virtual;
AContent
string
required
The string content to send
Result
THorseResponse
Returns the response instance for method chaining
Example:
THorse.Get('/hello',
  procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
  begin
    Res.Send('Hello, World!');
  end);

Send (Typed Object)

Send a typed object response (typically used with JSON middleware).
function Send<T>(AContent: T): THorseResponse; overload;
T
class
required
The type of the content object
AContent
T
required
The object to send
Result
THorseResponse
Returns the response instance for method chaining
Example:
type
  TUser = class
    Id: Integer;
    Name: string;
  end;

THorse.Get('/user',
  procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
  var
    User: TUser;
  begin
    User := TUser.Create;
    User.Id := 1;
    User.Name := 'John Doe';
    Res.Send<TUser>(User);
  end);

Status Methods

Status (Set with THTTPStatus)

Set the HTTP status code using the THTTPStatus enum.
function Status(const AStatus: THTTPStatus): THorseResponse; overload; virtual;
AStatus
THTTPStatus
required
The HTTP status code enum value
Result
THorseResponse
Returns the response instance for method chaining
Example:
THorse.Post('/users',
  procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
  begin
    // Create user
    Res.Status(THTTPStatus.Created).Send('User created');
  end);

THorse.Get('/notfound',
  procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
  begin
    Res.Status(THTTPStatus.NotFound).Send('Not Found');
  end);

Status (Set with Integer)

Set the HTTP status code using an integer value.
function Status(const AStatus: Integer): THorseResponse; overload; virtual;
AStatus
Integer
required
The HTTP status code (e.g., 200, 404, 500)
Result
THorseResponse
Returns the response instance for method chaining
Example:
THorse.Get('/custom',
  procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
  begin
    Res.Status(418).Send('I am a teapot');
  end);

Status (Get)

Get the current HTTP status code.
function Status: Integer; overload; virtual;
Result
Integer
Returns the current status code
Example:
THorse.Get('/status',
  procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
  var
    CurrentStatus: Integer;
  begin
    CurrentStatus := Res.Status;
    Res.Send('Current status: ' + IntToStr(CurrentStatus));
  end);

File Response Methods

SendFile (Stream)

Send a file from a stream to the client.
function SendFile(const AFileStream: TStream; const AFileName: string = ''; const AContentType: string = ''): THorseResponse; overload; virtual;
AFileStream
TStream
required
The file stream to send
AFileName
string
Optional file name for Content-Disposition header
AContentType
string
Optional content type (auto-detected from file extension if not provided)
Result
THorseResponse
Returns the response instance for method chaining
Example:
THorse.Get('/image',
  procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
  var
    FileStream: TFileStream;
  begin
    FileStream := TFileStream.Create('image.jpg', fmOpenRead);
    Res.SendFile(FileStream, 'image.jpg', 'image/jpeg');
  end);

SendFile (File Path)

Send a file from disk to the client.
function SendFile(const AFileName: string; const AContentType: string = ''): THorseResponse; overload; virtual;
AFileName
string
required
The path to the file to send
AContentType
string
Optional content type (auto-detected if not provided)
Result
THorseResponse
Returns the response instance for method chaining
Example:
THorse.Get('/document',
  procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
  begin
    Res.SendFile('C:\Files\document.pdf');
  end);

Download (Stream)

Trigger a file download from a stream.
function Download(const AFileStream: TStream; const AFileName: string; const AContentType: string = ''): THorseResponse; overload; virtual;
AFileStream
TStream
required
The file stream to download
AFileName
string
required
The file name to use in the download dialog
AContentType
string
Optional content type
Result
THorseResponse
Returns the response instance for method chaining
Example:
THorse.Get('/export',
  procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
  var
    DataStream: TMemoryStream;
  begin
    DataStream := TMemoryStream.Create;
    // Generate export data
    Res.Download(DataStream, 'export.csv', 'text/csv');
  end);

Download (File Path)

Trigger a file download from disk.
function Download(const AFileName: string; const AContentType: string = ''): THorseResponse; overload; virtual;
AFileName
string
required
The path to the file to download
AContentType
string
Optional content type
Result
THorseResponse
Returns the response instance for method chaining
Example:
THorse.Get('/report',
  procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
  begin
    Res.Download('C:\Reports\monthly-report.pdf');
  end);

Render (Stream)

Render an HTML file from a stream.
function Render(const AFileStream: TStream; const AFileName: string): THorseResponse; overload; virtual;
AFileStream
TStream
required
The HTML file stream
AFileName
string
required
The file name
Result
THorseResponse
Returns the response instance for method chaining
Example:
THorse.Get('/page',
  procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
  var
    HtmlStream: TFileStream;
  begin
    HtmlStream := TFileStream.Create('page.html', fmOpenRead);
    Res.Render(HtmlStream, 'page.html');
  end);

Render (File Path)

Render an HTML file from disk.
function Render(const AFileName: string): THorseResponse; overload; virtual;
AFileName
string
required
The path to the HTML file
Result
THorseResponse
Returns the response instance for method chaining
Example:
THorse.Get('/',
  procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
  begin
    Res.Render('C:\Web\index.html');
  end);

Header Methods

AddHeader

Add or update a response header.
function AddHeader(const AName, AValue: string): THorseResponse; virtual;
AName
string
required
The header name
AValue
string
required
The header value
Result
THorseResponse
Returns the response instance for method chaining
Example:
THorse.Get('/data',
  procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
  begin
    Res.AddHeader('X-Custom-Header', 'MyValue')
       .AddHeader('Cache-Control', 'no-cache')
       .Send('Data');
  end);

RemoveHeader

Remove a response header.
function RemoveHeader(const AName: string): THorseResponse; virtual;
AName
string
required
The header name to remove
Result
THorseResponse
Returns the response instance for method chaining
Example:
THorse.Get('/noheader',
  procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
  begin
    Res.RemoveHeader('X-Powered-By')
       .Send('Response without header');
  end);

Content Methods

Content (Get)

Get the response content object.
function Content: TObject; overload; virtual;
Result
TObject
Returns the content object
Example:
THorse.Get('/content',
  procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
  var
    ContentObj: TObject;
  begin
    ContentObj := Res.Content;
    // Work with content object
  end);

Content (Set)

Set the response content object.
function Content(const AContent: TObject): THorseResponse; overload; virtual;
AContent
TObject
required
The content object to set
Result
THorseResponse
Returns the response instance for method chaining
Example:
THorse.Get('/object',
  procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
  var
    DataObject: TMyData;
  begin
    DataObject := TMyData.Create;
    Res.Content(DataObject);
  end);

ContentType

Set the Content-Type header.
function ContentType(const AContentType: string): THorseResponse; virtual;
AContentType
string
required
The content type (e.g., ‘application/json’, ‘text/html’)
Result
THorseResponse
Returns the response instance for method chaining
Example:
THorse.Get('/xml',
  procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
  begin
    Res.ContentType('application/xml')
       .Send('<data>Hello</data>');
  end);

Redirect Method

RedirectTo

Redirect the client to a different URL.
function RedirectTo(const ALocation: string): THorseResponse; overload; virtual;
function RedirectTo(const ALocation: string; const AStatus: THTTPStatus): THorseResponse; overload; virtual;
ALocation
string
required
The URL to redirect to
AStatus
THTTPStatus
Optional HTTP status code (default: 303 See Other)
Result
THorseResponse
Returns the response instance for method chaining
Example:
// Simple redirect (303 See Other)
THorse.Get('/old-page',
  procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
  begin
    Res.RedirectTo('/new-page');
  end);

// Permanent redirect (301 Moved Permanently)
THorse.Get('/old-url',
  procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
  begin
    Res.RedirectTo('/new-url', THTTPStatus.MovedPermanently);
  end);

Raw Response Access

RawWebResponse

Get the underlying web response object.
function RawWebResponse: TWebResponse; virtual; // TResponse in FPC
Result
TWebResponse
Returns the underlying TWebResponse (Delphi) or TResponse (FPC) object
Example:
THorse.Get('/raw',
  procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
  var
    RawRes: TWebResponse;
  begin
    RawRes := Res.RawWebResponse;
    // Access low-level response properties
    Res.Send('OK');
  end);

See Also

Build docs developers (and LLMs) love