Search code examples
rcurlhttr

making specific request with httr


How would I make this request with httr?

'curl -X POST https://api.dropboxapi.com/2/files/list_folder \
    --header "Authorization: Bearer 21318318usdhsdha9283718 " \
    --header "Content-Type: application/json" \
    --data "{\"path\": \"/today/\",\"recursive\": false,\"include_media_info\": false,\"include_deleted\": false}"'

I have tried using curlconverter but hasn't worked well for this one. I am not sure how I would go about implementing the --data parameter and what follows.


Solution

  • this works for me, does it work for you?

    httr::POST(
      "https://api.dropboxapi.com/2/files/list_folder",
      add_headers(Authorization = "Bearer <token>"),
      content_type_json(),
      body = "{\"path\": \"/folder\",\"recursive\": false,\"include_media_info\": false,\"include_deleted\": false}",
      encode = "json"
    )
    

    If you want to generalize a bit for many folders:

    library("httr")
    foobar <- function(x) {
      content(POST(
        "https://api.dropboxapi.com/2/files/list_folder",
        add_headers(Authorization = "Bearer <token>"),
        content_type_json(),
        body = list(path = paste0("/", x), recursive = FALSE, 
                    include_media_info = FALSE, include_deleted = FALSE),
        encode = "json"
      ))
    }
    
    lapply(c('a', 'b', "c"), foobar)