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

#' 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::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"
) -> res1
trimws(rawToChar(res1$content))
} else {
writeLines(res, out)
perl <- find_perl()
args <- system.file("bin", "h2c.pl", package = "httr2curl")
processx::run(
command = perl,
args = args,
stdin = out
) -> res
trimws(res$stdout)
}
}