pr_static() with filename contains chinese character?

I put two test files under /data/download:test.txt , 测试.txt

After executing the following plumber code, visit
http://ip:8001/download/test.txt
http://ip:8001/download/ 测试.txt
Should be able to see the data,But the first one is normal, and the second one is 404
Even more strange is this 404 cannot be caught by pr_set_404()

Does it not support Chinese file names?
What should I do?

library(plumber)

handler_404 <- function(req, res) {
  print("---------")
  print(req)
  print("---------")
  res$status <- 404
  res$body <- "Oops! There's nothing you want"
}

pr() %>%
  pr_static("/download", "/data/download") %>%
  pr_run(port = 8001,  host = "0.0.0.0") %>% 
  pr_set_404(handler_404)

Hi, I believe pr_set_404 have to be called before pr_run. I was able to reproduce this behavior, it should be filed as an issue in the plumber github repo.

Filed issue, will see where that goes.

Maybe support Chinese character in req$PATH_INFO · Issue #753 · rstudio/plumber (github.com)

If you want a solution right now, you can use a filter to modify the request PATH_INFO before it is being used to fetch your file with a plumber filter. The filter will execute before the file is located on disk

library(plumber)

handler_404 <- function(req, res) {
  print("---------")
  print(req)
  print("---------")
  res$status <- 404
  res$body <- "Oops! There's nothing you want"
}

pr() %>%
  pr_static("/download", "/data/download") %>%
  pr_set_404(handler_404) %>%
  pr_filter("Support Chinese character", function(req) {
    req$PATH_INFO <- httpuv::decodeURIComponent(req$PATH_INFO)
    forward()
  }) %>%
  pr_run(port = 8001,  host = "0.0.0.0")
1 Like

Thank you! It works.
I should learn more about it!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.