Skip to content

Instantly share code, notes, and snippets.

@benjaminguinaudeau
Last active September 23, 2022 19:31
Show Gist options
  • Save benjaminguinaudeau/46652c61adee4469899a4026b873d07c to your computer and use it in GitHub Desktop.
Save benjaminguinaudeau/46652c61adee4469899a4026b873d07c to your computer and use it in GitHub Desktop.
Parse Curl Request from clipboard
parse_curl_request <- function(){
tot_req <- clipr::read_clip() %>%
stringr::str_split("\n") %>%
.[[1]]
url <- tot_req %>%
str_subset("^curl") %>%
str_extract("(?<=').*?(?=')")
tmp <- tot_req %>%
stringr::str_subset("-H") %>%
stringr::str_extract("(?<=').*?(?=')") %>%
stringr::str_split("\\:\\s+")
headers <- map_chr(tmp, 2)
names(headers) <- map_chr(tmp, 1)
r_code <- glue::glue("httr::GET(url = {url}, httr::add_headers(.headers = {paste(capture.output(dput(headers)), collapse = '')}))")
req <- try(httr::GET(url = url, httr::add_headers(.headers = headers)))
dt <- tibble(url = url, headers = list(headers), r_code, req = list(req))
return(dt)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment