HttpRequest
Inherits from: Object
An incoming HTTP request received by an HttpServer route handler.
HttpRequest objects are created by the server and passed to route handler blocks. They provide access to the request method, path, headers, query parameters, and body.
Example
"Inspecting a request in a handler"
server route: '/api/data' method: 'POST' handler: [:req |
| method <String> path <String> body <String> |
method := req method. "=> 'POST'"
path := req path. "=> '/api/data'"
body := req body. "=> request body string"
HttpResponse new: 200 body: body
]
Example
"Reading headers and query parameters"
server route: '/search' method: 'GET' handler: [:req |
| auth <String> query <String> |
auth := req header: 'Authorization'.
query := req queryParam: 'q'.
HttpResponse new: 200 body: 'Searching for: ', query
]
Instance Methods
primitives
body
Return the request body as a string. The body is read lazily on first access and cached for subsequent calls.
Example
| body <String> |
body := request body
header:
Return the value of the named HTTP header, or an empty string if the header is not present.
Example
| contentType <String> |
contentType := request header: 'Content-Type'
method
Return the HTTP method of the request (e.g. 'GET', 'POST').
Example
request method "=> 'GET'"
path
Return the URL path of the request (e.g. '/api/data').
Example
request path "=> '/api/data'"
queryParam:
Return the value of the named URL query parameter, or an empty string if the parameter is not present.
Example
"For URL '/search?q=hello'"
request queryParam: 'q' "=> 'hello'"