Closed lime-n closed 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))
Found my error:
def(url, APIkey, n-1)
Should have been:
def(APIkey, n-1)
I have built a recursive function to download GET requests from ordnance survey API endpoints. However, I find the following error:
After the first iteration inside the function, even though the headers remain consistent and the same.