Skip to content

Instantly share code, notes, and snippets.

@jeroen
Last active June 21, 2017 15:20
Show Gist options
  • Save jeroen/9bee7a7de99d48ff493acbc2598aa41e to your computer and use it in GitHub Desktop.
Save jeroen/9bee7a7de99d48ff493acbc2598aa41e to your computer and use it in GitHub Desktop.
Get the filename or final location of a URL without downloading
# To request without downloading body set CURLOPT_NODOBY = TRUE
handle <- curl::new_handle(nobody = TRUE)
url <- 'https://httpbin.org/redirect-to?url=http%3A%2F%2Fexample.com%2F'
req <- curl::curl_fetch_memory(url, handle)
print(req$url)
# To get the 'filename'
url <- 'http://pasta.lternet.edu/package/data/eml/knb-lter-ntl/320/4/4a283c25f3548c0f78d8a01658e4a353'
req <- curl::curl_fetch_memory(url, handle = handle)
grep("Content-Disposition:", curl::parse_headers(req$headers), value = TRUE)
@jsta
Copy link

jsta commented Jun 21, 2017

Thanks! I am pushing that output through stringr::str_extract(output, "(?<=\\=)(.*?)\\.csv") to get only the filename.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment