How to post within a rvest html_session?
Asked Answered
J

1

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

Junji answered 7/5, 2016 at 7:22 Comment(0)
J
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)
Junji answered 7/5, 2016 at 17:22 Comment(2)
This brilliant trick also helped me for this question.Subaxillary
@Rentrop: This request_POST method is now deprecated entirely in RVEST. What should be done in that case?Codie

© 2022 - 2024 — McMap. All rights reserved.