5

How can i post "within" a html session?

So after i opened a session via a <- rvest::html_session(url)

I tried:

library(httr)
POST(path, 
          add_headers(setNames(as.character(headers(a)), names(headers(a)))), 
          set_cookies(setNames(cookies(a)$value, cookies(a)$name)),
          body = list(...), 
          encode = "json")

But this handles my request as I were not logged in. Any suggestions? I am looking for something like POST(session, path, body, ...)

Arcoutte
  • 810
  • 4
  • 16
Rentrop
  • 20,979
  • 10
  • 72
  • 100

1 Answers1

10

Ok, after some digging into it i solved it by using:

x %>% rvest:::request_POST(url,
          config(referer = x$url),
          user_agent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.86 Safari/537.36"),
          body = list(...), 
          encode = "form")

Where rvest:::request_POST internally uses

httr::POST(url, x$config, ..., handle = x$handle)
Rentrop
  • 20,979
  • 10
  • 72
  • 100
  • This brilliant trick also helped me [for this question](https://stackoverflow.com/questions/40327657/). – Fr. Oct 30 '16 at 22:14
  • 2
    @Rentrop: This request_POST method is now deprecated entirely in RVEST. What should be done in that case? – Bharath Dec 09 '22 at 15:34