将文件作为输入参数的 API

我被要求创建一个 API,它将文件作为输入并将该文件放在服务器上的目录中。这是为了消除应用程序直接与文件夹交互的需要。

我之前已经构建了 API(在 R 中使用 Plumber)并且可以处理字符串输入——我只是很难将文件作为输入参数。管道工文档都没有解释如何做到这一点。这甚至可以做到吗?有没有办法在 Python 中做到这一点?


大话西游666
浏览 170回答 1
1回答

开心每一天1111

您可以使用plumber和Rook使用 POST 上传文件。这是一个小例子api.Rlibrary(plumber)library(Rook)#* Upload file#* @param upload File to upload#* @post /uploadfilefunction(req, res){&nbsp; fileInfo <- list(formContents = Rook::Multipart$parse(req))&nbsp; ## The file is downloaded in a temporary folder&nbsp; tmpfile <- fileInfo$formContents$upload$tempfile&nbsp; ## Copy the file to a new folder, with its original name&nbsp; fn <- file.path('~/Downloads', fileInfo$formContents$upload$filename)&nbsp; file.copy(tmpfile, fn)&nbsp; ## Send a message with the location of the file&nbsp; res$body <- paste0("Your file is now stored in ", fn, "\n")&nbsp; res}运行服务器plumber::plumb('api.R')$run(port = 1234)使用 curl 发送文件 test.txtcurl -v -F upload=@test.txt http://localhost:1234/uploadfile
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python