Last active
June 21, 2017 15:20
-
-
Save jeroen/9bee7a7de99d48ff493acbc2598aa41e to your computer and use it in GitHub Desktop.
Get the filename or final location of a URL without downloading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! I am pushing that output through
stringr::str_extract(output, "(?<=\\=)(.*?)\\.csv")
to get only the filename.