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;
The string content to send
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;
The type of the content object
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;
The HTTP status code enum value
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;
The HTTP status code (e.g., 200, 404, 500)
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;
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;
Optional file name for Content-Disposition header
Optional content type (auto-detected from file extension if not provided)
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;
The path to the file to send
Optional content type (auto-detected if not provided)
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;
The file stream to download
The file name to use in the download dialog
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;
The path to the file to download
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;
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;
The path to the HTML file
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);
Add or update a response header.
function AddHeader(const AName, AValue: string): THorseResponse; virtual;
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);
Remove a response header.
function RemoveHeader(const AName: string): THorseResponse; virtual;
The header name to remove
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;
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;
The content object to set
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;
The content type (e.g., ‘application/json’, ‘text/html’)
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;
Optional HTTP status code (default: 303 See Other)
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
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