mannym mannym - 9 days ago 5
R Question

passing through special characters in password when using download.file

I am trying to download a file using the

download.file
function. The URL I am using is password protected. My password however has multiple special characters in it and it is not letting me download my file. Is there a way I can bypass this without changing the password?

The password I am using includes the special characters '$' '@'

Below is the example and the error code I am getting.

download.file("http://myusername:P@$$word@myurl.com?$format=json", myfile.json, method = "auto")


'InternetOpenUrl failed: 'The server name or address could not be resolved'

Answer

Try encoding the characters in your password:

password <- "P@$$sword"
password <- URLencode(password, reserved=TRUE)  # deal with @ and $

file.loc <- sprintf("http://myusername:%s@myurl.com?format=json", password)
download.file(file.loc, ...)