Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing user agent string in a http request in R

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.

like image 356
Soumendra Avatar asked Dec 27 '10 05:12

Soumendra


2 Answers

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().

like image 80
Droplet Avatar answered Oct 23 '22 11:10

Droplet


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.

like image 25
Marek Avatar answered Oct 23 '22 11:10

Marek