I am currently working on a project involving a Plumber API, and I've encountered a challenge related to handling CSV files in the body of HTTP requests.
I have a Plumber API where I need to receive 2 CSV files in the body of a POST request sent from Postman (for example). I am unsure how to do that ?
I've read about Plumber Parsers and there was a parser_csv but couldn't figure out how to use that to get that data from request body !!
#* Process two files in the request body
#* @post /process_files
#* @parser multipart/form-data
function(req, res) {
if (is.null(req$files) || length(req$files) < 2) {
res$status <- 400
res$body <- '{"error": "Two files must be included in the request."}'
return()
}
file1 <- req$files$file1
file2 <- req$files$file2
## here I process the data
res$status <- 200
res$body <- '{"message": "Files processed successfully."}'
}
The answer below could probably help you understand plumber parsers?
A solution similar to this?
#* Process two files in the request body
#* @post /process_files
#* @param files:[file]
#* @parser multi
#* @parser csv
function(files, req, res) {
if (length(files) < 2) {
res$status <- 400
res$body <- '{"error": "Two files must be included in the request."}'
return()
}
file1 <- files[[1]]
file2 <- files[[2]]
## here I process the data
res$status <- 200
res$body <- '{"message": "Files processed successfully."}'
}