Plumber - POST a zip file

Dear community,
I'm trying to figure out how to execute a POST request in plumber where in the body I upload a zip file that must then be accepted by plumber and proceed to do the unzip process.

This is the code that i am using corrently which report the following error output

2023-07-01 16:48:57.177 - POST /data - libcurl/7.84.0 r-curl/5.0.0 httr/1.4.6 @ 127.0.0.1 
 127.0.0.1 
 80 
<simpleError in rawToChar(value): embedded nul in string: 'PK\003\004\024\0\b\b\b\0\xaa4\xe1V\0\0\0\0\0\0\0\0\0\0\0\0\r\0\0\0shapefile.cpg\001\005\0\xfa\xffUTF-8PK\a\bP<\x81\016\n\0\0\0\005\0\0\0PK\003\004\024\0\b\b\b\0\xaa4\xe1V\0\0\0\0\0\0\0\0\0\0\0\0\r\0\0\0shapefile.dbfuÉ˝\t\xc0 \030\x84\xe1\v\026\xa9B\xda\xc4\xca\t\xe4S\\\xc0\005\034\xc0\xdeV\apy\xf1\xa7\021\xf5-\x8e\x83\x87\xe5\xfbb\0,~\xec\xf2)\x86q]\x9d\xef\x9d\xfd\021-\xa5I\xd2\\\aM\a\020f\001^\0PK\a\b0\xe9k\x89C\0\0\0\x8d\0\0\0PK\003\004\024\0\b\b\b\0\xaa4\xe1V\0\0\0\0\0\0\0\0\0\0\0\0\r\0\0\0shapefile.prjE\xcc1\v\002!\030\xc6\xf1\xef\xe2,\xa2\xbez\xea\030)\xe6p\xddq\xde\xd1 \xe2\020R\021\030\004q_?iiy\x86\037<\177\xef&\177\x8c\t\xf5)\x9f\xf6l\xaf\xbd!l\017\xeb6&d\xcb\xc5\xc7\u008c\026\b\xc7\xf9\xe4\x96)Ř„\xfe6\x80\xd2\f\024\xa1\x98\033M\xb8T\x9c\x83\034 g</at\xbd\xe0ßµ\xb6\xfdq\xbd#L\t\xcdx;\x87\xb5w\xeb\xad\xfb\x8f\x98\022\022\xb8\xe1\x92\031#\xa0_\xbfPK\a\bE]\x935\177\0\0\0\x90\0\0\0PK\003\004\024\0\b\b\b\0\xaa4\xe1V\0\0\0\0\0\0\0\0\0\0\0\0\r\0\0\0shapefil>

The following is the Plumber.R code

library(plumber)

#* Log some information about the incoming request
#* @filter logger
function(req){
  
  cat(as.character(Sys.time()), "-",
      req$REQUEST_METHOD, req$PATH_INFO, "-",
      req$HTTP_USER_AGENT, "@", req$REMOTE_ADDR, "\n",
      req$SERVER_NAME, "\n",
      req$SERVER_PORT, "\n")
  
  plumber::forward()
  
}

#* @post /data
function(req) {
  
  #browser()
  
  filename <- names(req$postBody)
  content <- req$postBody
  writeBin(content, "ciao.zip")
  unzip("ciao.zip")
}

and this is the rcode to perform the post request.

api_url <- "http://localhost:80/data?"

r <- httr::POST(
  api_url,
  body=httr::upload_file("~/ciao.zip")
)

Thanks to anyone help me to figure out this issue.

You should either change you server code (the one that handles upload requests) or your client code (the one that performs the actual upload requests).

On the server-side you're trying to read file contents from the HTTP request body, while on the client-side you're actually “submitting” a form with file upload field. See the actual code for httr::upload_file:

Check the Plumber docs, especially Routing & Input • plumber

I suppose you should use body or argsBody instead of postBody.

Probably something like that

library(plumber)

#* @post /data
#* @param upload:file
function(upload) { 
  filename <- names(upload)
  content <- upload[[1]]
  writeBin(content, "ciao.zip")
  unzip("ciao.zip")
}
curl -X POST "{api_host_url}/data" -H "accept: */*" -H "Content-Type: multipart/form-data" -F "upload=@ciao.zip;type=application/x-zip-compressed"
1 Like

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.