r-lib / httr

httr: a friendly http package for R
https://httr.r-lib.org
Other
986 stars 1.99k forks source link

is.character(headers) is not TRUE #736

Closed lime-n closed 1 year ago

lime-n commented 1 year ago

I have built a recursive function to download GET requests from ordnance survey API endpoints. However, I find the following error:

Error in request(headers = c(..., .headers)) :
is.character(headers) is not TRUE

After the first iteration inside the function, even though the headers remain consistent and the same.

library(httr)
APIkey = 'aExig8vlZqsuSGa6hFbxySxB4ySa0YOE'
response <- GET('https://api.os.uk/downloads/v1/products', add_headers(key = APIkey))

ScaleColourRaster <- httr::content(response)[[1]]$url
OpenMapLocal <- httr::content(response)[[9]]$url
BoundaryLine <- httr::content(response)[[2]]$url

options(timeout = max(600, getOption("timeout")))
def <- function(APIkey, n = 0, ...){
  if(n == 0) return()
  print(n)
  response <- GET(...elt(1)[n], add_headers(key = APIkey))
  response_<- GET(httr::content(response)$downloadsUrl, add_headers(key = APIkey))
  temp_url <- httr::content(response_)[[1]]$url
  match_and_group <- stringr::str_match_all(temp_url, '\\/([^\\/]+)\\/*')[[1]]
  file_name <- match_and_group[,2][nrow(match_and_group)]
  download.file(temp_url, destfile = paste0('data/',file_name, '.zip'))
  if(n > 0)def(url, APIkey, n-1)
}

def(APIkey,n=3,c(ScaleColourRaster, OpenMapLocal, BoundaryLine))
lime-n commented 1 year ago

Found my error:

def(url, APIkey, n-1)

Should have been:

def(APIkey, n-1)