R: managing httr2 request handles (and their cookies)

303 Views Asked by At

The predecessor R library of "httr2", that is "httr", provides methods to manage the requests handles.

The low level approach is:

h <- httr::handle("https://google.com/")
res <- httr::GET(handle = h)

Now, request settings are tied to h. For example, a new request keeps the same cookies.

(cs <- httr::cookies(res))
identical(httr::cookies(res), cs) # TRUE

To start from scratch, one resets the handle simply by generating a new one. That is:

h <- httr::handle("https://google.com/")
identical(httr::cookies(res), cs) # FALSE

As a (better) alternative, one can use the default handle and handle_reset():

c1 <- httr::cookies(httr::GET("https://google.com/"))
c2 <- httr::cookies(httr::GET("https://google.com/"))
identical(c1, c2) # TRUE

httr::handle_reset("https://google.com/")
c1 <- httr::cookies(httr::GET("https://google.com/"))
identical(c1, c2) # FALSE

Resetting the handle is necessary when you are testing authentications to websites and the state is kept in the cookies.

When it comes to "httr2", the manual doesn't even mention the handles. So I wonder how we can reset a connection, other than detaching the package and loading it again.

2

There are 2 best solutions below

0
Yannick Marcon On

There is this documentation about how to preserve cookies between across requests.

Then, if you provide different cookies file paths, you'll get separate request session states.

0
HUANG Xinzhuo On

You can extract the cookies manually.

res <- httr2::request("your website") |> 
   httr2::req_perform()

cookies <- purrr::pluck(res, "request", "headers", "Cookie")