Created
February 18, 2022 19:47
-
-
Save gadenbuie/04374d128f7a7731968de85b0c34e90e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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