Debian Codegraph @

man/curl_fetch.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/fetch.R
3 \name{curl_fetch_memory}
4 \alias{curl_fetch_memory}
5 \alias{curl_fetch_disk}
6 \alias{curl_fetch_stream}
7 \alias{curl_fetch_multi}
8 \alias{curl_fetch_echo}
9 \alias{handle_data}
10 \title{Fetch the contents of a URL}
11 \usage{
12 curl_fetch_memory(url, handle = new_handle())
13
14 curl_fetch_disk(url, path, handle = new_handle())
15
16 curl_fetch_stream(url, fun, handle = new_handle())
17
18 curl_fetch_multi(
19 url,
20 done = NULL,
21 fail = NULL,
22 pool = NULL,
23 data = NULL,
24 handle = new_handle()
25 )
26
27 curl_fetch_echo(url, handle = new_handle())
28
29 handle_data(handle)
30 }
31 \arguments{
32 \item{url}{A character string naming the URL of a resource to be downloaded.}
33
34 \item{handle}{A curl handle object.}
35
36 \item{path}{Path to save results}
37
38 \item{fun}{Callback function. Should have one argument, which will be
39 a raw vector.}
40
41 \item{done}{callback function for completed request. Single argument with
42 response data in same structure as \link{curl_fetch_memory}.}
43
44 \item{fail}{callback function called on failed request. Argument contains
45 error message.}
46
47 \item{pool}{a multi handle created by \link{new_pool}. Default uses a global pool.}
48
49 \item{data}{(advanced) callback function, file path, or connection object for writing
50 incoming data. This callback should only be used for \emph{streaming} applications,
51 where small pieces of incoming data get written before the request has completed. The
52 signature for the callback function is \code{write(data, final = FALSE)}. If set
53 to \code{NULL} the entire response gets buffered internally and returned by in
54 the \code{done} callback (which is usually what you want).}
55 }
56 \description{
57 Low-level bindings to write data from a URL into memory, disk or a callback
58 function.
59 }
60 \details{
61 The \verb{curl_fetch_*()} functions automatically raise an error upon protocol problems
62 (network, disk, TLS, etc.) but do not implement application logic. For example,
63 you need to check the status code of HTTP requests in the response by yourself,
64 and deal with it accordingly.
65
66 Both \code{curl_fetch_memory()} and \code{curl_fetch_disk} have a blocking and a
67 non-blocking C implementation. The latter is slightly slower but allows for
68 interrupting the download prematurely (using e.g. CTRL+C or ESC). Interrupting
69 is enabled when R runs in interactive mode or when
70 \code{getOption("curl_interrupt") == TRUE}.
71
72 The \code{curl_fetch_multi()} function is the asynchronous equivalent of
73 \code{curl_fetch_memory()}. It wraps \code{\link[=multi_add]{multi_add()}} to
74 schedule requests which are executed concurrently when calling
75 \code{\link[=multi_run]{multi_run()}}\code{}. For each successful request, the
76 \code{done} callback is triggered with response data. For failed requests
77 (when \code{curl_fetch_memory()} would raise an error), the \code{fail} function
78 is triggered with the error message.
79
80 After a request has been performed, metadata from the request can be read
81 from the handle object using \code{handle_data()} (this same information also gets
82 returned by \code{curl_fetch_memory()} directly). It includes things like:
83 \itemize{
84 \item Final URL (after redirects)
85 \item HTTP status code
86 \item Content-type
87 \item Response headers
88 \item Timings
89 \item Http-version
90 This data remains available in the handle until it is either re-used for a
91 new request, or \code{handle_reset()} is called.
92 }
93 }
94 \examples{
95 \donttest{
96 # Load in memory
97 res <- curl_fetch_memory("https://hb.cran.dev/cookies/set?foo=123&bar=ftw")
98 res$content
99
100 # Save to disk
101 res <- curl_fetch_disk("https://hb.cran.dev/stream/10", tempfile())
102 res$content
103 readLines(res$content)
104
105 # Stream with callback
106 drip_url <- "https://hb.cran.dev/drip?duration=3&numbytes=15&code=200"
107 res <- curl_fetch_stream(drip_url, function(x){
108 cat(rawToChar(x))
109 })
110
111 # Async API
112 data <- list()
113 success <- function(res){
114 cat("Request done! Status:", res$status, "\n")
115 data <<- c(data, list(res))
116 }
117 failure <- function(msg){
118 cat("Oh noes! Request failed!", msg, "\n")
119 }
120 curl_fetch_multi("https://hb.cran.dev/get", success, failure)
121 curl_fetch_multi("https://hb.cran.dev/status/418", success, failure)
122 curl_fetch_multi("https://urldoesnotexist.xyz", success, failure)
123 multi_run()
124 str(data)
125 }
126 }