'How can I use httr to bulk upload files to documentcloud

I want to use documentcloud's API to bulk upload a file folder of pdfs via R's httr package. I also want to receive a dataframe of the URLS of the uploaded files.

I figured out how to generate a token, but I can't get anything to upload successfully. Here is my attempt to upload a single pdf:

library(httr)
library(jsonlite)

url <- "https://api.www.documentcloud.org/api/documents/"

# Generate a token
user <- "username"

pw <- "password"

response <- POST("https://accounts.muckrock.com/api/token/",
    body= list(username = user,
     password = pw)
     )

token <- content(response)

access_token <- unlist(token$access)

paste <- paste("Bearer", access_token)


# Initiate upload for single pdf

POST(url,
      add_headers(`Authorization` = paste),
      body = upload_file("filename.PDF",
                         type = "application/pdf"),
     verbose()
    )



I get a 415 "Unsupported Media Type" error when attempting to initiate the upload for a single pdf. Unsure why this happens, and also once this is resolved, how I can bulk-upload many pdfs?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source