How does one change user agent strings in http requests made in R? And how do I figure out what my current user agent string looks like?
Thanks in advance.
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()
.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With