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

Aperta
#92 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
68/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
r
Ambito
api

Direzione di ricerca

Start by running the minimal plumber2 app from the report and compare the handlers using response$status, response$set_header(), and a plain response return. Trace the response formatting path that calls as.character(); done means a 302 response with the Location header is returned without an internal server error, including when the handler returns the response object.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
    })
}
Lingua principale
R
Stelle
119
Fork
17
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di posit-dev/plumber2

Tutte le issue di posit-dev/plumber2

Issue simili

Altre issue su R

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.