Changing user agent string in a http request in R

options("HTTPUserAgent") or getOption("HTTPUserAgent") prints your current settings, and options(HTTPUserAgent="My settings") is the way to change it.

To temporary change this option use: withr::with_options:

withr::with_options(list(HTTPUserAgent="My settings"), download.file(..something..))

or Droplet answer if you use download.file.


The solution using options() in the accepted answer will change the setting globally for the whole session (unless you change it back).

To change the User-Agent temporarily in a request made by download.file(), you need to use the headers argument:

download.file("https://httpbin.org/user-agent", 
              destfile = "test.txt", 
              headers = c("User-Agent" = "My Custom User Agent"))

Since R 4.0.0, you can also use this argument in available.packages() and install.packages() and it will be forwarded to download.file().