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