Overview
THorseRequest represents an HTTP request and provides access to headers, query parameters, route parameters, body content, cookies, and more.
type
THorseRequest = class
private
FWebRequest: TWebRequest; // TRequest in FPC
// ...
public
// Methods and properties
end;
Body Methods
Body (Get String)
Get the raw request body as a string.
function Body: string; overload; virtual;
Returns the raw request body content
Example:
THorse.Post('/data',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
var
RawBody: string;
begin
RawBody := Req.Body;
Writeln('Received: ' + RawBody);
Res.Send('OK');
end);
Body (Get Typed Object)
Get the request body as a typed object.
function Body<T: class>: T; overload;
The class type to cast the body to
Returns the body as an instance of type T
Example:
type
TUser = class
Name: string;
Email: string;
end;
THorse.Post('/users',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
var
User: TUser;
begin
User := Req.Body<TUser>;
try
Writeln('User: ' + User.Name);
Res.Send('User created');
finally
User.Free;
end;
end);
Body (Set Object)
Set the request body object (typically used by middleware).
function Body(const ABody: TObject): THorseRequest; overload; virtual;
The object to set as the request body
Returns the request instance for method chaining
Example:
// Middleware that parses JSON body
THorse.Use(
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
var
User: TUser;
begin
User := TJson.JsonToObject<TUser>(Req.Body);
Req.Body(User); // Set parsed object
Next;
end);
Parameter Access Methods
Get access to HTTP headers.
function Headers: THorseCoreParam; virtual;
Returns a parameter collection containing all request headers
Example:
THorse.Get('/info',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
var
ContentType: string;
begin
ContentType := Req.Headers['Content-Type'];
Res.Send('Content-Type: ' + ContentType);
end);
Query
Get access to query string parameters.
function Query: THorseCoreParam; virtual;
Returns a parameter collection containing query string parameters
Example:
// GET /search?q=horse&limit=10
THorse.Get('/search',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
var
SearchQuery: string;
Limit: string;
begin
SearchQuery := Req.Query['q']; // 'horse'
Limit := Req.Query['limit']; // '10'
Res.Send('Searching for: ' + SearchQuery);
end);
Params
Get access to route parameters.
function Params: THorseCoreParam; virtual;
Returns a parameter collection containing route parameters
Example:
// GET /users/123/posts/456
THorse.Get('/users/:userId/posts/:postId',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
var
UserId: string;
PostId: string;
begin
UserId := Req.Params['userId']; // '123'
PostId := Req.Params['postId']; // '456'
Res.Send('User: ' + UserId + ', Post: ' + PostId);
end);
Cookie
Get access to cookies.
function Cookie: THorseCoreParam; virtual;
Returns a parameter collection containing cookies
Example:
THorse.Get('/profile',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
var
SessionId: string;
begin
SessionId := Req.Cookie['sessionId'];
Res.Send('Session: ' + SessionId);
end);
ContentFields
Get access to form data fields (multipart/form-data or application/x-www-form-urlencoded).
function ContentFields: THorseCoreParam; virtual;
Returns a parameter collection containing form fields and uploaded files
Example:
THorse.Post('/upload',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
var
Username: string;
FileStream: TStream;
begin
Username := Req.ContentFields['username'];
FileStream := Req.ContentFields.Field('file').AsStream;
// Process file upload
Res.Send('File uploaded by: ' + Username);
end);
Session Methods
Session (Get Typed Session)
Get the session object as a typed instance.
function Session<T: class>: T; overload;
The class type to cast the session to
Returns the session as an instance of type T
Example:
type
TUserSession = class
UserId: Integer;
Username: string;
end;
THorse.Get('/dashboard',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
var
UserSession: TUserSession;
begin
UserSession := Req.Session<TUserSession>;
Res.Send('Welcome, ' + UserSession.Username);
end);
Session (Set Session Object)
Set the session object.
function Session(const ASession: TObject): THorseRequest; overload; virtual;
The object to set as the session
Returns the request instance for method chaining
Example:
THorse.Post('/login',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
var
UserSession: TUserSession;
begin
UserSession := TUserSession.Create;
UserSession.UserId := 123;
UserSession.Username := 'john';
Req.Session(UserSession);
Res.Send('Logged in');
end);
Sessions
Get access to the sessions manager.
function Sessions: THorseSessions; virtual;
Returns the sessions manager instance
MethodType
Get the HTTP method type of the request.
function MethodType: TMethodType; virtual;
Returns the HTTP method (mtGet, mtPost, mtPut, mtDelete, mtPatch, mtHead)
Example:
THorse.All('/*',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
case Req.MethodType of
mtGet: Writeln('GET request');
mtPost: Writeln('POST request');
mtPut: Writeln('PUT request');
mtDelete: Writeln('DELETE request');
end;
Next;
end);
ContentType
Get the Content-Type header value.
function ContentType: string; virtual;
Returns the Content-Type header value
Example:
THorse.Post('/data',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
if Req.ContentType = 'application/json' then
// Process JSON
else
Res.Status(415).Send('Unsupported Media Type');
end);
Host
Get the host name from the request.
function Host: string; virtual;
Example:
THorse.Get('/info',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
Res.Send('Host: ' + Req.Host);
end);
PathInfo
Get the request path.
function PathInfo: string; virtual;
Example:
THorse.Use(
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
Writeln('Request path: ' + Req.PathInfo);
Next;
end);
RawWebRequest
Get the underlying web request object.
function RawWebRequest: TWebRequest; virtual; // TRequest in FPC
Returns the underlying TWebRequest (Delphi) or TRequest (FPC) object
Example:
THorse.Get('/raw',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
var
RawReq: TWebRequest;
begin
RawReq := Req.RawWebRequest;
// Access low-level request properties
Res.Send('OK');
end);
See Also