You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.5 KiB
71 lines
1.5 KiB
#' Convert an 'httr' call to 'curl' command line
|
|
#'
|
|
#' @param complete_httr_verb_call wrap an `httr` `VERB` call with this function
|
|
#' and it will return the text of a working `curl` command line
|
|
#' @param use_web_service if `TRUE` (`FALSE` by default) this function will use
|
|
#' `https://curl.se/h2c/` to process the headers.
|
|
#' @export
|
|
#' @examples \dontrun{
|
|
#' h2c(
|
|
#' httr::GET(
|
|
#' url = "https://rud.is/",
|
|
#' httr::user_agent(splashr::ua_apple_tv),
|
|
#' query = list(
|
|
#' a = "b",
|
|
#' c = 1
|
|
#' )
|
|
#' )
|
|
#' )
|
|
#' }
|
|
h2c <- function(complete_httr_verb_call, use_web_service = FALSE) {
|
|
|
|
ƒ_call <- substitute(complete_httr_verb_call)
|
|
|
|
capture.output(
|
|
capture.output(
|
|
httr::with_verbose(
|
|
eval(ƒ_call)
|
|
), type = "message") -> res
|
|
) -> junk
|
|
|
|
out <- tempfile()
|
|
|
|
res[grepl("^->", res)] %>%
|
|
sub("^-> ", "", .) %>%
|
|
paste0(collapse = "\n") -> res
|
|
|
|
if (use_web_service) {
|
|
|
|
httr::POST(
|
|
url = "https://curl.se/h2c/",
|
|
httr:::content_type_json(),
|
|
body = list(
|
|
http = res
|
|
),
|
|
encode = "form"
|
|
) -> res1
|
|
|
|
pg <- httr::content(res1)
|
|
|
|
rvest::html_nodes(pg, xpath = ".//p[contains(., 'Command:')]/following-sibling::div[1]") %>%
|
|
rvest::html_text() %>%
|
|
trimws()
|
|
|
|
} else {
|
|
|
|
writeLines(res, out)
|
|
|
|
perl <- find_perl()
|
|
|
|
args <- system.file("bin", "h2c.pl", package = "httr2curl")
|
|
|
|
processx::run(
|
|
command = perl,
|
|
args = args,
|
|
stdin = out
|
|
) -> res
|
|
|
|
res$stdout
|
|
}
|
|
|
|
}
|