Skip to contents

If after is NULL, sends a 303 response and halts request processing. Client is redirected to the given location. This is commonly used in Post/Redirect/GET (PRG) setups to redirect clients to a new page following form submissions.

Usage

redirect(response, location, after = NULL)

Arguments

response

reqres::Response object

location

path or url to redirect to

after

optional number of seconds to wait before redirection

Value

Details

If after is a numeric, a "Refresh" header is attached to the response, instructing the browser to navigate to location after the specified number of seconds.

The delayed redirect uses the non-standard "Refresh" HTTP header which is widely supported by browsers but is not part of the official HTTP specification. It should not be relied on for API & non-browser clients.

See also:

Examples

# Immediate redirect (PRG pattern)
#* @get /
function(response) {
    print("Hello!")
    redirect(response, "/foo")
}
#> function (response) 
#> {
#>     print("Hello!")
#>     redirect(response, "/foo")
#> }
#> <environment: 0x55d2ddde9ea0>

# Delayed redirect after rendering content
#* @get /count/<n>
function(n, response) {
  redirect(response, "/", after = 1)
  paste("n =", n)
}
#> function (n, response) 
#> {
#>     redirect(response, "/", after = 1)
#>     paste("n =", n)
#> }
#> <environment: 0x55d2ddde9ea0>