plumber
plumber copied to clipboard
Connection is closed on OPTIONS requests
Setting the Connection
header for CORS preflight requests does not work with Plumber
#* @filter cors
cors <- function(res) {
res$setHeader("Access-Control-Allow-Origin", "*")
res$setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
res$setHeader("Connection", "Keep-Alive")
res$setHeader("Keep-Alive", "timeout=300")
plumber::forward()
}
#* @options /opt
options <- function() {}
#* @post /opt
optimizeData <- function() {...}
The OPTIONS
response comes back with the headers:
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Origin: *
Connection: close
Keep-Alive: timeout=300
and the browser doesn't send the subsequent POST
request
This no longer the case, and I'm pretty sure it was fixed in #318.
In the plumber.R
file, you need to add a filter that handles OPTIONS CORS preflight requests from web browsers and returns code 200 and an empty list(); the code is here https://github.com/rstudio/plumber/issues/66#issuecomment-418660334