Creates an HTTP route handler for use with http_server().
Arguments
- path
URI path to match (e.g., "/api/data", "/users").
- callback
Function to handle requests. Receives a list with:
method- HTTP method (character)uri- Request URI (character)headers- Named character vector of headersbody- Request body (raw vector)
Should return a list with:
status- HTTP status code (integer, default 200)headers- Response headers as a named character vector, e.g.c("Content-Type" = "application/json")(optional)body- Response body (character or raw)
- method
[default "GET"] HTTP method to match (e.g., "GET", "POST", "PUT", "DELETE"). Use
"*"to match any method.- prefix
[default FALSE] Logical, if TRUE matches path as a prefix (e.g., "/api" will match "/api/users", "/api/items", etc.).
Value
A handler object for use with http_server().
Details
If the callback throws an error, a 500 Internal Server Error response is returned to the client.
See also
handler_ws() for WebSocket handlers. Static handlers:
handler_file(), handler_directory(), handler_inline(),
handler_redirect().
Examples
# Simple GET handler
h1 <- handler("/hello", function(req) {
list(status = 200L, body = "Hello!")
})
# POST handler that echoes the request body
h2 <- handler("/echo", function(req) {
list(status = 200L, body = req$body)
}, method = "POST")
# Catch-all handler for a path prefix
h3 <- handler("/static", function(req) {
# Serve static files under /static/*
}, method = "*", prefix = TRUE)
