| 1 |
## ----echo = FALSE, message = FALSE------------------------------------------------------------------------------------ |
| 2 |
knitr::opts_chunk$set(comment = "") |
| 3 |
options(width = 120, max.print = 100) |
| 4 |
wrap.simpleError <- function(x, options) { |
| 5 |
paste0("```\n## Error: ", x$message, "\n```") |
| 6 |
} |
| 7 |
library(curl) |
| 8 |
library(jsonlite) |
| 9 |
|
| 10 |
## --------------------------------------------------------------------------------------------------------------------- |
| 11 |
req <- curl_fetch_memory("https://hb.cran.dev/get?foo=123") |
| 12 |
str(req) |
| 13 |
parse_headers(req$headers) |
| 14 |
jsonlite::prettify(rawToChar(req$content)) |
| 15 |
|
| 16 |
## --------------------------------------------------------------------------------------------------------------------- |
| 17 |
tmp <- tempfile() |
| 18 |
curl_download("https://hb.cran.dev/get?bar=456", tmp) |
| 19 |
jsonlite::prettify(readLines(tmp)) |
| 20 |
|
| 21 |
## --------------------------------------------------------------------------------------------------------------------- |
| 22 |
con <- curl("https://hb.cran.dev/get") |
| 23 |
open(con) |
| 24 |
|
| 25 |
# Get 3 lines |
| 26 |
out <- readLines(con, n = 3) |
| 27 |
cat(out, sep = "\n") |
| 28 |
|
| 29 |
# Get 3 more lines |
| 30 |
out <- readLines(con, n = 3) |
| 31 |
cat(out, sep = "\n") |
| 32 |
|
| 33 |
# Get remaining lines |
| 34 |
out <- readLines(con) |
| 35 |
close(con) |
| 36 |
cat(out, sep = "\n") |
| 37 |
|
| 38 |
## ----eval=FALSE------------------------------------------------------------------------------------------------------- |
| 39 |
# # This httpbin mirror doesn't cache |
| 40 |
# con <- curl("https://nghttp2.org/httpbin/drip?duration=1&numbytes=50") |
| 41 |
# open(con, "rb", blocking = FALSE) |
| 42 |
# while(isIncomplete(con)){ |
| 43 |
# buf <- readBin(con, raw(), 1024) |
| 44 |
# if(length(buf)) |
| 45 |
# cat("received: ", rawToChar(buf), "\n") |
| 46 |
# } |
| 47 |
# close(con) |
| 48 |
|
| 49 |
## --------------------------------------------------------------------------------------------------------------------- |
| 50 |
pool <- new_pool() |
| 51 |
success <- function(req){cat("success:", req$url, ": HTTP:", req$status, "\n")} |
| 52 |
failure <- function(err){cat("failure:", err, "\n")} |
| 53 |
curl_fetch_multi('https://www.google.com', done = success, fail = failure, pool = pool) |
| 54 |
curl_fetch_multi('https://cloud.r-project.org', done = success, fail = failure, pool = pool) |
| 55 |
curl_fetch_multi('https://hb.cran.dev/blabla', done = success, fail = failure, pool = pool) |
| 56 |
curl_fetch_multi('https://doesnotexit.xyz', done = success, fail = failure, pool = pool) |
| 57 |
|
| 58 |
## --------------------------------------------------------------------------------------------------------------------- |
| 59 |
# This actually performs requests: |
| 60 |
out <- multi_run(pool = pool) |
| 61 |
print(out) |
| 62 |
|
| 63 |
## --------------------------------------------------------------------------------------------------------------------- |
| 64 |
# This is OK |
| 65 |
curl_download('https://cloud.r-project.org/CRAN_mirrors.csv', 'mirrors.csv') |
| 66 |
mirros <- read.csv('mirrors.csv') |
| 67 |
unlink('mirrors.csv') |
| 68 |
|
| 69 |
## ----error=TRUE------------------------------------------------------------------------------------------------------- |
| 70 |
try({ |
| 71 |
# Oops! A typo in the URL! |
| 72 |
curl_download('https://cloud.r-project.org/CRAN_mirrorZ.csv', 'mirrors.csv') |
| 73 |
con <- curl('https://cloud.r-project.org/CRAN_mirrorZ.csv') |
| 74 |
open(con) |
| 75 |
}) |
| 76 |
|
| 77 |
## ----echo = FALSE, message = FALSE, warning=FALSE--------------------------------------------------------------------- |
| 78 |
close(con) |
| 79 |
rm(con) |
| 80 |
|
| 81 |
## --------------------------------------------------------------------------------------------------------------------- |
| 82 |
req <- curl_fetch_memory('https://cloud.r-project.org/CRAN_mirrors.csv') |
| 83 |
print(req$status_code) |
| 84 |
|
| 85 |
## --------------------------------------------------------------------------------------------------------------------- |
| 86 |
# Oops a typo! |
| 87 |
req <- curl_fetch_disk('https://cloud.r-project.org/CRAN_mirrorZ.csv', 'mirrors.csv') |
| 88 |
print(req$status_code) |
| 89 |
|
| 90 |
# This is not the CSV file we were expecting! |
| 91 |
head(readLines('mirrors.csv')) |
| 92 |
unlink('mirrors.csv') |
| 93 |
|
| 94 |
## ----error=TRUE------------------------------------------------------------------------------------------------------- |
| 95 |
try({ |
| 96 |
h <- new_handle(failonerror = TRUE) |
| 97 |
curl_fetch_memory('https://cloud.r-project.org/CRAN_mirrorZ.csv', handle = h) |
| 98 |
}) |
| 99 |
|
| 100 |
## --------------------------------------------------------------------------------------------------------------------- |
| 101 |
h <- new_handle() |
| 102 |
handle_setopt(h, copypostfields = "moo=moomooo"); |
| 103 |
handle_setheaders(h, |
| 104 |
"Content-Type" = "text/moo", |
| 105 |
"Cache-Control" = "no-cache", |
| 106 |
"User-Agent" = "A cow" |
| 107 |
) |
| 108 |
|
| 109 |
## --------------------------------------------------------------------------------------------------------------------- |
| 110 |
handle <- new_handle(verbose = TRUE) |
| 111 |
|
| 112 |
## ----error = TRUE----------------------------------------------------------------------------------------------------- |
| 113 |
try({ |
| 114 |
# URLOPT_MASFILESIZE must be a number |
| 115 |
handle_setopt(handle, maxfilesize = "foo") |
| 116 |
|
| 117 |
# CURLOPT_USERAGENT must be a string |
| 118 |
handle_setopt(handle, useragent = 12345) |
| 119 |
}) |
| 120 |
|
| 121 |
## --------------------------------------------------------------------------------------------------------------------- |
| 122 |
curl::curl_symbols("CURLUSESSL") |
| 123 |
|
| 124 |
## --------------------------------------------------------------------------------------------------------------------- |
| 125 |
handle_setopt(handle, use_ssl = 3) |
| 126 |
|
| 127 |
## --------------------------------------------------------------------------------------------------------------------- |
| 128 |
curl_symbols('CURL_HTTP_VERSION_') |
| 129 |
|
| 130 |
## --------------------------------------------------------------------------------------------------------------------- |
| 131 |
# Force using HTTP 1.1 (the number 2 is an enum value, see above) |
| 132 |
handle_setopt(handle, http_version = 2) |
| 133 |
|
| 134 |
## --------------------------------------------------------------------------------------------------------------------- |
| 135 |
req <- curl_fetch_memory("https://hb.cran.dev/post", handle = h) |
| 136 |
jsonlite::prettify(rawToChar(req$content)) |
| 137 |
|
| 138 |
## --------------------------------------------------------------------------------------------------------------------- |
| 139 |
con <- curl("https://hb.cran.dev/post", handle = h) |
| 140 |
jsonlite::prettify(readLines(con)) |
| 141 |
|
| 142 |
## ----echo = FALSE, message = FALSE, warning=FALSE--------------------------------------------------------------------- |
| 143 |
close(con) |
| 144 |
|
| 145 |
## --------------------------------------------------------------------------------------------------------------------- |
| 146 |
tmp <- tempfile() |
| 147 |
curl_download("https://hb.cran.dev/post", destfile = tmp, handle = h) |
| 148 |
jsonlite::prettify(readLines(tmp)) |
| 149 |
|
| 150 |
## --------------------------------------------------------------------------------------------------------------------- |
| 151 |
curl_fetch_multi("https://hb.cran.dev/post", handle = h, done = function(res){ |
| 152 |
cat("Request complete! Response content:\n") |
| 153 |
cat(rawToChar(res$content)) |
| 154 |
}) |
| 155 |
|
| 156 |
# Perform the request |
| 157 |
out <- multi_run() |
| 158 |
|
| 159 |
## --------------------------------------------------------------------------------------------------------------------- |
| 160 |
# Start with a fresh handle |
| 161 |
h <- new_handle() |
| 162 |
|
| 163 |
# Ask server to set some cookies |
| 164 |
req <- curl_fetch_memory("https://hb.cran.dev/cookies/set?foo=123&bar=ftw", handle = h) |
| 165 |
req <- curl_fetch_memory("https://hb.cran.dev/cookies/set?baz=moooo", handle = h) |
| 166 |
handle_cookies(h) |
| 167 |
|
| 168 |
# Unset a cookie |
| 169 |
req <- curl_fetch_memory("https://hb.cran.dev/cookies/delete?foo", handle = h) |
| 170 |
handle_cookies(h) |
| 171 |
|
| 172 |
## --------------------------------------------------------------------------------------------------------------------- |
| 173 |
req1 <- curl_fetch_memory("https://hb.cran.dev/get") |
| 174 |
req2 <- curl_fetch_memory("https://www.google.com") |
| 175 |
|
| 176 |
## --------------------------------------------------------------------------------------------------------------------- |
| 177 |
req <- curl_fetch_memory("https://api.github.com/users/ropensci") |
| 178 |
req$times |
| 179 |
|
| 180 |
req2 <- curl_fetch_memory("https://api.github.com/users/rstudio") |
| 181 |
req2$times |
| 182 |
|
| 183 |
## --------------------------------------------------------------------------------------------------------------------- |
| 184 |
handle_reset(h) |
| 185 |
|
| 186 |
## --------------------------------------------------------------------------------------------------------------------- |
| 187 |
# Posting multipart |
| 188 |
h <- new_handle() |
| 189 |
handle_setform(h, |
| 190 |
foo = "blabla", |
| 191 |
bar = charToRaw("boeboe"), |
| 192 |
iris = form_data(serialize(iris, NULL), "application/rda"), |
| 193 |
description = form_file(system.file("DESCRIPTION")), |
| 194 |
logo = form_file(file.path(R.home('doc'), "html/logo.jpg"), "image/jpeg") |
| 195 |
) |
| 196 |
req <- curl_fetch_memory("https://hb.cran.dev/post", handle = h) |
| 197 |
|
| 198 |
## ----eval=getRversion() > "4.1"--------------------------------------------------------------------------------------- |
| 199 |
# Perform request |
| 200 |
res <- new_handle() |> |
| 201 |
handle_setopt(copypostfields = "moo=moomooo") |> |
| 202 |
handle_setheaders("Content-Type"="text/moo", "Cache-Control"="no-cache", "User-Agent"="A cow") |> |
| 203 |
curl_fetch_memory(url = "https://hb.cran.dev/post") |
| 204 |
|
| 205 |
# Parse response |
| 206 |
res$content |> rawToChar() |> jsonlite::prettify() |
| 207 |
|