rredcap

Error importing a file into a file repository using R and REDCap API


I'm using the REDCap API to try to upload a file into a file repository within a data access group:

library(RCurl)


#!/usr/bin/env Rscript
token <- "xyz123456"
url <- "https://redcap.myredcap/api/"
file <- '//example/my_directory/file_example.html'
formData <- list("token"=token,
                 action='import',
                 content='fileRepository',
                 folder_id=1,
                 returnFormat='json',
                 file=file
)
response <- httr::POST(url, body = formData, encode = "multipart")
result <- httr::content(response)
print(result)

Get this error:

$error
[1] "No valid file was uploaded"

How can I resolve this?


Solution

  • Try replacing file = file with file = httr::upload_file(file) - this works for me when I directly upload files to RC (though difficult to be reproducible, I just tested it on one of my projects and it worked (though not specific to a DAG, but the only differences in the code is I dont have folder_id = 1):

    formData <- list(
      "token" = token,
      action = 'import',
      content = 'fileRepository',
      folder_id = 1,
      returnFormat = 'json',
      file = httr::upload_file(file)
    )