HttpRequest PRO
The HttpRequest class represents an HTTP request received by the server.
It encapsulates all request information, including path, method, headers, body, client address, and parsed parameters.
An instance of this class is provided as an argument to handler functions registered through HttpServer.registerHandler().
Overview
HttpRequest is typically used inside HTTP route handlers to:
- Access request path, method, headers, and body
- Read URL parameters and query parameters
- Parse form data (
application/x-www-form-urlencodedormultipart/form-data) - Verify authentication tokens or custom headers
Properties
path: string
The request path (excluding query parameters).
Example:
method: string
The HTTP method of the request (e.g., "GET", "POST", "PUT", "DELETE").
Example:
headers: Record<string, string>
An object containing all HTTP request headers as key–value pairs.
Example:
body: Data
The request body as a Data object.
You can convert it to a string using methods such as toRawString("utf-8").
Example:
address: string | null
The IP address of the client that made the request.
If the address cannot be determined, this value is null.
Example:
params: Record<string, string>
An object containing route parameters defined in the registered path.
Example:
If the request path is /user/123, the response will be:
queryParams: Array<{ key: string; value: string }>
An array of query parameter key–value pairs extracted from the request URL.
Example:
Methods
hasTokenForHeader(headerName: string, token: string): boolean
Checks whether the specified header contains the given token value.
Commonly used for authentication headers such as Authorization.
Parameters:
Returns:
trueif the header contains the tokenfalseotherwise
Example:
parseUrlencodedForm(): Array<{ key: string; value: string }>
Parses the request body as application/x-www-form-urlencoded form data.
Typically used for HTML form submissions via POST.
Returns: An array of key–value pairs representing form fields.
Example:
If the body is:
then the output is:
parseMultiPartFormData(): Array<{ name: string | null; filename: string | null; headers: Record<string, string>; data: Data }>
Parses multipart/form-data requests, typically used for file uploads.
Returns: An array of form parts, where each element contains:
Example:
Full Example
The example below shows how to read request data and respond accordingly:
