I would like to expose an endpoint that accepts multipart/form-data, parses the multipart content and returns a csv-file. (the multipart input contains a csv dataset and processing instructions)
I’ve done this with plumber using Rook::Multipart$parse()
as suggested here. Because plumber doesn’t support parallel requests, I’d like to re-implement this with RestRserve. The following won’t work – plumber’s inputs are of class environment
(which Rook::Multipart$parse()
assumes) whereas RestRserve’s inputs are of class Request R6
.
application = Application$new(content_type = "text/plain")
application$add_post("/echo", function(req, res) {
multipart <- Rook::Multipart$parse(req$body)
dta <- read_csv(multipart$dta$tempfile, trim_ws=FALSE)
res$set_body(dta)
})
Any ideas on how to get multipart/form-data input to work with RestRserve?
RestRserve
parses multipart body when process the incoming request. As result you have a rawrequest$body
and metatdata in therequest$files
.Request
object also provides aget_file
method to extract body content. Let me show example for the app and request:See
?Request
for more details about fields and methods in this class.