Debian Codegraph @

man/getBinaryURL.Rd

raw
1 \name{getBinaryURL}
2 \alias{getBinaryURL}
3 \title{Download binary content}
4 \description{
5 This function allows one to download binary content.
6 This is a convenience function that is a call to
7 \code{\link{getURL}} with suitable values
8 for the \code{write} and \code{file} options
9 for the Curl handle. These take care of processing
10 the body of the response to the Curl request into a
11 vector of "raw" elements.
12
13 Binary content from POST forms or other requests that are not simple
14 URL requests can be implemented using the same approach as this
15 function, i.e., specifying the same values as in the body of this function for
16 \code{write} and \code{file} in the call to \code{\link{curlPerform}}.
17
18 }
19 \usage{
20 getBinaryURL(url, ..., .opts = list(), curl = getCurlHandle(),
21 .buf = binaryBuffer(.len), .len = 5000)
22 }
23 %- maybe also 'usage' for other objects documented here.
24 \arguments{
25 \item{url}{the URL identifying the content to download.
26 This can be a regular URL or a
27 \code{application/x-www-form-urlencoded} URL,
28 i.e. with name=value parameters appended to the location via a ?,
29 and separated from each other via a &.}
30 \item{\dots}{additional arguments that are passed to \code{\link{getURL}}.}
31 \item{.opts}{a list of named values that are passed to
32 \code{\link{getURL}} as the \code{.opts} argument.}
33 \item{curl}{an optional curl handle used in \code{\link{curlPerform}}
34 that has been created previously and is to be reused for this
35 request.
36 This allows the R user to reuse a curl handle that already has a
37 connection to the server or has settings for options that have been
38 set previously.}
39 \item{.buf}{a raw vector in which to insert the body of the response.
40 This is a parameter to allow the caller to reuse an existing buffer.}
41 \item{.len}{an non-negative integer which is used as the length for
42 the buffer in which to store the binary data in the response.
43 The buffer is extended if it is not big enough but this allows the
44 caller to provide context specific knowledge about the length of the
45 response, e.g. the size of the file being downloaded, and
46 avoid expanding the buffer as the material is being processed.
47 }
48 }
49 \value{
50 A "raw" vector.
51 }
52 \author{Duncan Temple Lang}
53 \seealso{
54 \code{\link{getURL}},
55 \code{\link[base]{raw}},
56 \code{\link{memDecompress}}
57 }
58 \examples{
59 u = "https://aitap.grebedoc.dev/RCurl/data.gz"
60
61 tryCatch(withAutoprint({
62
63 content = getBinaryURL(u)
64
65 if (getRversion() >= "4") withAutoprint({
66 x <- memDecompress(content, asChar = TRUE)
67 read.csv(textConnection(x))
68 }) else withAutoprint({
69 tmp = tempfile()
70 writeBin(content, con = tmp)
71 read.csv(gzfile(tmp))
72 unlink(tmp)
73 })
74
75
76 # Working from the Content-Type in the header of the HTTP response.
77 h = basicTextGatherer()
78 content = getBinaryURL(u, .opts = list(headerfunction = h$update))
79 header = parseHTTPHeader(h$value())
80 type = strsplit(header["Content-Type"], "/")[[1]]
81
82 if(type[2] \%in\% c("x-gzip", "gzip")) {
83 if (getRversion() >= "4") {
84 cat(memDecompress(content, asChar = TRUE))
85 } else {
86 tmp = tempfile()
87 writeBin(content, con = tmp)
88 writeLines(readLines(gzfile(tmp)))
89 unlink(tmp)
90 }
91 }
92
93 }), GenericCurlError = identity)
94 }
95 \keyword{IO}
96 \keyword{programming}
97 \concept{binary data}