Search code examples
rweb-scrapingrvesthttr

How to post within a rvest html_session?


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


Solution

  • 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)