| handler | R Documentation |
Creates an HTTP route handler for use with http_server().
handler(path, callback, method = "GET", prefix = FALSE)
path |
URI path to match (e.g., "/api/data", "/users"). |
callback |
Function to handle requests. Receives a list with:
Should return a list with:
|
method |
[default "GET"] HTTP method to match (e.g., "GET", "POST",
"PUT", "DELETE"). Use |
prefix |
[default FALSE] Logical, if TRUE matches path as a prefix (e.g., "/api" will match "/api/users", "/api/items", etc.). |
If the callback throws an error, a 500 Internal Server Error response is returned to the client.
A handler object for use with http_server().
handler_ws() for WebSocket handlers. Static handlers:
handler_file(), handler_directory(), handler_inline(),
handler_redirect().
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.