Skip to content

Response cannot be returned because it's being parsed to character #92

Open

Description

@AlbertRapp

According to the documentation, the response object can be returned:

Image

But when I try to run this minimal plumber app:

library(plumber2)

#* @plumber
function(api) {
  api |>
    api_logger(
      logger_console(),
      access_log_format = plumber2::combined_log_format
    ) |>
    api_get("/", function(request, response) {
      response$status <- 302L
      response$set_header("Location", "echo/hello123")
    })
}

#* Echo the parameter that was sent in
#*
#* @get /echo/<msg>
#*
#* @param msg:string The message to echo back.
#*
#* @response 200:{msg:string} A string containing the input message
#*
function(msg) {
  msg
}

I get "internal server errors" instead of the redirect to echo/hello123 and can see in the logs the following errors messages:

2026-07-07 13:59:04.411662 - error: Error formatting the response body
2026-07-07 13:59:04.411662 - error: Caused by error in `as.character()`:
2026-07-07 13:59:04.411662 - error: ! cannot coerce type 'closure' to vector of type 'character'

This happens because response$set_header() returns the modified response object instead of NULL. So if I were first set the header and then the status, then everything works as expected (because the assignment returns NULL):

#* @plumber
function(api) {
  api |>
    api_logger(
      logger_console(),
      access_log_format = plumber2::combined_log_format
    ) |>
    api_get("/", function(request, response) {
      response$set_header("Location", "echo/hello123")
      response$status <- 302L # <<<<<<<<<<<< This returns NULL
    })
}

I tried to investigate if this is due to modification of set_header() that returning a response object erros but this code still causes the same error:

#* @plumber
function(api) {
  api |>
    api_logger(
      logger_console(),
      access_log_format = plumber2::combined_log_format
    ) |>
    api_get("/", function(request, response) {
      response # <<<<<<<<<< Returning the plain response object
    })
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions