Skip to content

Instantly share code, notes, and snippets.

@gadenbuie
Created February 18, 2022 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gadenbuie/04374d128f7a7731968de85b0c34e90e to your computer and use it in GitHub Desktop.
Save gadenbuie/04374d128f7a7731968de85b0c34e90e to your computer and use it in GitHub Desktop.
library(httr2)
# setup and perform the request for a file
req <- request("https://placekitten.com/200/300") %>%
req_perform()
# The response body contains raw data,
# so we read that into a vector (into memory so can't be super big)
resp_binary <- resp %>% resp_body_raw()
# Write the file somewhere using writeBin, here using a temp file
tmpfile <- tempfile(fileext = ".jpg")
writeBin(resp_binary, tmpfile)
# check it out: a kitten!
fs::file_show(tmpfile)
# could also do this
tmpfile2 <- tempfile(fileext = "jpg")
request("https://placekitten.com/200/300") %>%
req_perform(path = tmpfile2)
fs::file_show(tmpfile2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment