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.

68 lines
1.5 KiB

3 years ago
#' 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
3 years ago
#' @param use_web_service if `TRUE` (`FALSE` by default) this function will use
#' `https://curl.se/h2c/` to process the headers.
3 years ago
#' @export
#' @examples \dontrun{
#' h2c(
#' httr::GET(
#' url = "https://rud.is/",
#' httr::user_agent(splashr::ua_apple_tv),
#' query = list(
#' a = "b",
#' c = 1
#' )
#' )
#' )
#' }
3 years ago
h2c <- function(complete_httr_verb_call, use_web_service = FALSE) {
3 years ago
ƒ_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("^-> ", "", .) %>%
3 years ago
paste0(collapse = "\n") -> res
if (use_web_service) {
httr::POST(
url = "https://curl.se/h2c/",
3 years ago
httr::user_agent("curl/7.64.1"),
httr::accept("*/*"),
httr::content_type("application/x-www-form-urlencoded"),
body = sprintf("http=%s", curl::curl_escape(res)),
encode = "raw"
3 years ago
) -> res1
3 years ago
trimws(rawToChar(res1$content))
3 years ago
} else {
writeLines(res, out)
perl <- find_perl()
args <- system.file("bin", "h2c.pl", package = "httr2curl")
3 years ago
3 years ago
processx::run(
command = perl,
args = args,
stdin = out
) -> res
3 years ago
3 years ago
trimws(res$stdout)
3 years ago
}
3 years ago
}