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

String

body

""

String

client_ip

""

int

client_port

0

Dictionary

headers

{}

String

method

""

String

path

""

Dictionary

path_params

{}

Dictionary

query_params

{}

String

raw_path

""

Methods

String

get_header(name: String, default_value: String = "") const

String

get_path_param(name: String, default_value: String = "") const

String

get_query_param(name: String, default_value: String = "") const

bool

has_header(name: String) const

bool

has_path_param(name: String) const

bool

has_query_param(name: String) const

Dictionary

parse_form_data() const

Dictionary

parse_json_body() const


Property Descriptions

String body = "" 🔗

The raw request body content as a string.


String client_ip = "" 🔗

The IP address of the client that made the request.


int client_port = 0 🔗

  • void set_client_port(value: int)

  • int get_client_port()

The port number of the client connection.


Dictionary headers = {} 🔗

Dictionary containing all HTTP headers from the request. Header names are lowercase.


String method = "" 🔗

The HTTP method of the request (GET, POST, PUT, DELETE, etc.).


String path = "" 🔗

The request path without query parameters.


Dictionary path_params = {} 🔗

Dictionary containing path parameters extracted from the route pattern using {variable} syntax.


Dictionary query_params = {} 🔗

Dictionary containing query parameters from the URL.


String raw_path = "" 🔗

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.