Changing user agent string in a http request in R
Asked Answered
B

2

11

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.

Berton answered 27/12, 2010 at 5:50 Comment(0)
U
16

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.

Ultan answered 27/12, 2010 at 6:45 Comment(0)
A
6

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

Adjacency answered 29/4, 2020 at 15:20 Comment(1)
Where can I find suitable user agent strings for this?Brook

© 2022 - 2024 — McMap. All rights reserved.