-
Notifications
You must be signed in to change notification settings - Fork 261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No suitable parser found to handle request body type application/vnd.ms-excel. #959
Comments
How are you sending the data? If you send the data as a |
Similar to
#* @serializer contentType list(type="application/vnd.ms-excel") .
You'll also need to transform your output into an excel file before returning it in your function. (I do not know how to do this last step.) |
A possible approach which adds a # untested!
register_serializer(
"xlsx",
function(..., type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") {
if (!requireNamespace("writexl", quietly = TRUE)) {
stop("The writexl package is not available but is required in order to use the writexl serializer",
call. = FALSE)
}
serializer_write_file(
# The temp file should end in `.xlsx`
fileext = ".xlsx",
# Pass through your content type
type = type,
write_fn = function(val, tmpfile) {
writexl::write_xlsx(x = val, path =tmpfile, ...)
}
)
}) |
@schloerke I thought
@chuxinyuan I think you don't need to use #* @post /upload
#* @param f:file
#* @parser multi
#* @parser csv
function(req, res, f) {
browser() # for demonstration
data <- f[[1]]
data
} Execution: print(f)
# $out.csv
# # A tibble: 3 × 11
# mpg cyl disp hp drat wt qsec vs am gear carb
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
# 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
# 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1 So you can use FYI, the command-line for uploading the file (for this test): curl -X 'POST' 'http://127.0.0.1:9999/v1/upload' -F '[email protected];type=text/csv' Alternatively, if you want to be able to work with the raw CSV string, remove the A simple reprex: #* @post /upload
#* @param f:file
function(req, res, f) {
browser()
data <- read.csv(text = f[[1]])
data
} Execution: print(f)
# $out.csv
# [1] "mpg,cyl,disp,hp,drat,wt,qsec,vs,am,gear,carb\n21,6,160,110,3.9,2.62,16.46,0,1,4,4\n21,6,160,110,3.9,2.875,17.02,0,1,4,4\n22.8,4,108,93,3.85,2.32,18.61,1,1,4,1\n"
data <- read.csv(f[[1]])
# Warning in file(file, "rt") :
# cannot open file 'mpg,cyl,disp,hp,drat,wt,qsec,vs,am,gear,carb
# 21,6,160,110,3.9,2.62,16.46,0,1,4,4
# 21,6,160,110,3.9,2.875,17.02,0,1,4,4
# 22.8,4,108,93,3.85,2.32,18.61,1,1,4,1
# ': No such file or directory
# Error in file(file, "rt") : cannot open the connection
data <- read.csv(text = f[[1]])
print(data)
# mpg cyl disp hp drat wt qsec vs am gear carb
# 1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
# 2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
# 3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 ... and then do with |
@schloerke seems like a simple PR with this code might be welcome? (Unrelated to this issue.) |
@r2evans Yes, Thomas and I both mis-read as we were just talking about serializers. Thank you! **Re-opening
@r2evans Yes, can you please make a PR? |
@chuxinyuan I believe you want something similar to... # untested
parser_xlsx <- function(...) {
parser_read_file(function(tmpfile) {
read_xl:: read_excel(tmpfile, ...)
})
}
plumber::register_parser("xlsx", parser_xlsx, fixed = "application/vnd.ms-excel", regex = "^application/vnd\.ms-excel") @r2evans When a parser can be paired with a serializer, it makes for a good combo. Can I ask for you to include it as well? Thank you! |
Already working on it :-) |
Description
I want to upload the data in .csv format, but I had trouble deploying it to the server.
Code
plumber_run.R
plumber.R
Data
Session info
The text was updated successfully, but these errors were encountered: