7

httr::GET preserves cookies when making requests to the same website.

  1. Is it possible to query those preserved cookies?
  2. How can I flush those preserved cookies and make "pristine" requests again?

Example:

# Get login cookie
r1 <- GET("https://some.url/login", authenticate("foo", "bar"))

cookies(r1)
# returns a data frame of two cookies

# Make request that requires authentication cookie
# Only succeeds if r1 was made
r2 <- GET("https://some.url/data/?query&subset=1")
r2

Notice that when making r2 you dont have to pass any cookie information explicitely as they are stored somewhere automatically.

I would like to know how these stored cookies can be queried or deleted?

Michał
  • 2,755
  • 1
  • 17
  • 20
  • 1
    I'm not sure about clearing existing cookies, but see the example in `?httr::cookies` for querying them. – nrussell Oct 11 '16 at 14:42
  • Thanks @nrussell `cookies()` is not exactly what I meant. But I have added an example to clarify things. – Michał Oct 11 '16 at 15:03

3 Answers3

8

Use a new handle to request.

h1 <- handle('')
r1 <- GET("https://some.url/login", handle=h1, authenticate("foo", "bar"))

h2 <- handle('')
r2 <- GET("https://some.url/data/?query&subset=1", handle=h2)
Timespace
  • 5,101
  • 7
  • 23
  • 32
0

One (very round about) way is to "reset" the package:

detach("package:httr", unload=TRUE)
library(httr)

I'm still looking for something better.

BonStats
  • 358
  • 2
  • 9
  • yeah... that's pretty blunt. Problematic too if you need to have other packages depending on `httr` loaded. – Michał Jan 01 '17 at 22:58
  • I'd be interested to see if you come up with something, I'm still trying to figure something out for myself... I tried looking at the global settings httr has but no luck so far – BonStats Jan 02 '17 at 23:08
0

This worked for me:

# Get login cookie
r1 <- GET("https://some.url/login", authenticate("foo", "bar"))

cookies(r1)
# The cookies should be there, then:
handle_reset("https://some.url")
cookies(r1)
#now the cookies should be removed

I hope it helps

Siel347
  • 1
  • 2