HTTPRequestContext
Inherits: RefCounted < Object
Container for HTTP request data passed to route handlers.
Description
HTTPRequestContext encapsulates all data from an HTTP request including method, path, headers, query parameters, path parameters, and body content. It is automatically created by HTTPServer and passed to route handler callbacks.
HTTPServer.register_route("GET", "/api/users/{id}", func(req: HTTPRequestContext, res):
# Access path parameter
var user_id = req.get_path_param("id")
# Access query parameter
var format = req.get_query_param("format", "json")
# Access header
var auth = req.get_header("authorization", "")
# Get client info
print("Request from: ", req.get_client_ip())
)
Tutorials
Properties
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Methods
get_header(name: String, default_value: String = "") const |
|
get_path_param(name: String, default_value: String = "") const |
|
get_query_param(name: String, default_value: String = "") const |
|
has_header(name: String) const |
|
has_path_param(name: String) const |
|
has_query_param(name: String) const |
|
parse_form_data() const |
|
parse_json_body() const |
Property Descriptions
The raw request body content as a string.
The IP address of the client that made the request.
The port number of the client connection.
Dictionary headers = {} 🔗
void set_headers(value: Dictionary)
Dictionary get_headers()
Dictionary containing all HTTP headers from the request. Header names are lowercase.
The HTTP method of the request (GET, POST, PUT, DELETE, etc.).
The request path without query parameters.
Dictionary path_params = {} 🔗
void set_path_params(value: Dictionary)
Dictionary get_path_params()
Dictionary containing path parameters extracted from the route pattern using {variable} syntax.
Dictionary query_params = {} 🔗
void set_query_params(value: Dictionary)
Dictionary get_query_params()
Dictionary containing query parameters from the URL.
The complete request path including query parameters.
Method Descriptions
String get_header(name: String, default_value: String = "") const 🔗
Returns the value of the HTTP header with the given name, or default_value if the header is not present. Header names are case-insensitive.
String get_path_param(name: String, default_value: String = "") const 🔗
Returns the value of a path parameter extracted from the route pattern. For example, with pattern "/api/users/{id}" and request "/api/users/123", calling get_path_param("id") returns "123".
var route_params = {}
route_params = ctx.get_path_params()
var user_id = route_params.get("id", "")
String get_query_param(name: String, default_value: String = "") const 🔗
Returns the value of a query parameter from the URL. For example, for "/api/users?id=5&format=json", calling get_query_param("id") returns "5".
bool has_header(name: String) const 🔗
Returns true if the request contains an HTTP header with the given name. Header names are case-insensitive.
bool has_path_param(name: String) const 🔗
Returns true if a path parameter with the given name exists.
bool has_query_param(name: String) const 🔗
Returns true if a query parameter with the given name exists.
Dictionary parse_form_data() const 🔗
Parses the request body as application/x-www-form-urlencoded data and returns a dictionary of key-value pairs. Returns an empty dictionary if parsing fails.
Dictionary parse_json_body() const 🔗
Parses the request body as JSON and returns a dictionary. Returns an empty dictionary if the body is not valid JSON or is not a JSON object.