Create HTTP requests
This is an R6 class from the package R6. Find out more
about R6 at https://r6.r-lib.org/. After creating an instance of an R6
class (e.g., x <- HttpClient$new(url = "https://hb.opencpu.org")) you can
access values and methods on the object x.
url(character) a url
opts(list) named list of curl options
proxiesa proxy() object
authan auth() object
headers(list) named list of headers, see http-headers
handlea handle()
progressonly supports httr::progress(), see progress
payloadresulting payload after request
xself
...ignored
new()Create a new HttpRequest object
HttpRequest$new(url, opts, proxies, auth, headers, handle, progress)url(character) A url. One of url or handle required.
optsany curl options
proxiesa proxy() object
authan auth() object
headersnamed list of headers, see http-headers
handlea handle()
progressonly supports httr::progress(), see progress
urls(character) one or more URLs
A new HttpRequest object
get()Define a GET request
HttpRequest$get(
  path = NULL,
  query = list(),
  disk = NULL,
  stream = NULL,
  mock = getOption("crul_mock", NULL),
  ...
)pathURL path, appended to the base URL
queryquery terms, as a named list
diska path to write to. if NULL (default), memory used.
See curl::curl_fetch_disk() for help.
streaman R function to determine how to stream data. if
NULL (default), memory used. See curl::curl_fetch_stream()
for help
mockA mocking function. If supplied, this function is called
with the request. It should return either NULL (if it doesn't want to
handle the request) or a HttpResponse (if it does).
...curl options, only those in the acceptable set from
curl::curl_options() except the following: httpget, httppost, post,
postfields, postfieldsize, and customrequest
post()Define a POST request
HttpRequest$post(
  path = NULL,
  query = list(),
  body = NULL,
  disk = NULL,
  stream = NULL,
  encode = "multipart",
  mock = getOption("crul_mock", NULL),
  ...
)pathURL path, appended to the base URL
queryquery terms, as a named list
bodybody as an R list
diska path to write to. if NULL (default), memory used.
See curl::curl_fetch_disk() for help.
streaman R function to determine how to stream data. if
NULL (default), memory used. See curl::curl_fetch_stream()
for help
encodeone of form, multipart, json, or raw
mockA mocking function. If supplied, this function is called
with the request. It should return either NULL (if it doesn't want to
handle the request) or a HttpResponse (if it does).
...curl options, only those in the acceptable set from
curl::curl_options() except the following: httpget, httppost, post,
postfields, postfieldsize, and customrequest
put()Define a PUT request
HttpRequest$put(
  path = NULL,
  query = list(),
  body = NULL,
  disk = NULL,
  stream = NULL,
  encode = "multipart",
  mock = getOption("crul_mock", NULL),
  ...
)pathURL path, appended to the base URL
queryquery terms, as a named list
bodybody as an R list
diska path to write to. if NULL (default), memory used.
See curl::curl_fetch_disk() for help.
streaman R function to determine how to stream data. if
NULL (default), memory used. See curl::curl_fetch_stream()
for help
encodeone of form, multipart, json, or raw
mockA mocking function. If supplied, this function is called
with the request. It should return either NULL (if it doesn't want to
handle the request) or a HttpResponse (if it does).
...curl options, only those in the acceptable set from
curl::curl_options() except the following: httpget, httppost, post,
postfields, postfieldsize, and customrequest
patch()Define a PATCH request
HttpRequest$patch(
  path = NULL,
  query = list(),
  body = NULL,
  disk = NULL,
  stream = NULL,
  encode = "multipart",
  mock = getOption("crul_mock", NULL),
  ...
)pathURL path, appended to the base URL
queryquery terms, as a named list
bodybody as an R list
diska path to write to. if NULL (default), memory used.
See curl::curl_fetch_disk() for help.
streaman R function to determine how to stream data. if
NULL (default), memory used. See curl::curl_fetch_stream()
for help
encodeone of form, multipart, json, or raw
mockA mocking function. If supplied, this function is called
with the request. It should return either NULL (if it doesn't want to
handle the request) or a HttpResponse (if it does).
...curl options, only those in the acceptable set from
curl::curl_options() except the following: httpget, httppost, post,
postfields, postfieldsize, and customrequest
delete()Define a DELETE request
HttpRequest$delete(
  path = NULL,
  query = list(),
  body = NULL,
  disk = NULL,
  stream = NULL,
  encode = "multipart",
  mock = getOption("crul_mock", NULL),
  ...
)pathURL path, appended to the base URL
queryquery terms, as a named list
bodybody as an R list
diska path to write to. if NULL (default), memory used.
See curl::curl_fetch_disk() for help.
streaman R function to determine how to stream data. if
NULL (default), memory used. See curl::curl_fetch_stream()
for help
encodeone of form, multipart, json, or raw
mockA mocking function. If supplied, this function is called
with the request. It should return either NULL (if it doesn't want to
handle the request) or a HttpResponse (if it does).
...curl options, only those in the acceptable set from
curl::curl_options() except the following: httpget, httppost, post,
postfields, postfieldsize, and customrequest
head()Define a HEAD request
HttpRequest$head(path = NULL, mock = getOption("crul_mock", NULL), ...)pathURL path, appended to the base URL
mockA mocking function. If supplied, this function is called
with the request. It should return either NULL (if it doesn't want to
handle the request) or a HttpResponse (if it does).
...curl options, only those in the acceptable set from
curl::curl_options() except the following: httpget, httppost, post,
postfields, postfieldsize, and customrequest
verb()Use an arbitrary HTTP verb supported on this class Supported verbs: get, post, put, patch, delete, head
HttpRequest$verb(verb, ...)verban HTTP verb supported on this class: get, post, put, patch, delete, head. Also supports retry.
...curl options, only those in the acceptable set from
curl::curl_options() except the following: httpget, httppost, post,
postfields, postfieldsize, and customrequest
z <- HttpRequest$new(url = "https://hb.opencpu.org/get")
res <- z$verb('get', query = list(hello = "world"))
res$payload
retry()Define a RETRY request
HttpRequest$retry(
  verb,
  ...,
  pause_base = 1,
  pause_cap = 60,
  pause_min = 1,
  times = 3,
  terminate_on = NULL,
  retry_only_on = NULL,
  onwait = NULL
)verban HTTP verb supported on this class: get, post, put, patch, delete, head. Also supports retry.
...curl options, only those in the acceptable set from
curl::curl_options() except the following: httpget, httppost, post,
postfields, postfieldsize, and customrequest
pause_base, pause_cap, pause_minbasis, maximum, and minimum for
calculating wait time for retry. Wait time is calculated according to the
exponential backoff with full jitter algorithm. Specifically, wait time is
chosen randomly between pause_min and the lesser of pause_base * 2 and
pause_cap, with pause_base doubling on each subsequent retry attempt.
Use pause_cap = Inf to not terminate retrying due to cap of wait time
reached.
timesthe maximum number of times to retry. Set to Inf to
not stop retrying due to exhausting the number of attempts.
terminate_on, retry_only_ona vector of HTTP status codes. For
terminate_on, the status codes for which to terminate retrying, and for
retry_only_on, the status codes for which to retry the request.
onwaita callback function if the request will be retried and a wait time is being applied. The function will be passed two parameters, the response object from the failed request, and the wait time in seconds. Note that the time spent in the function effectively adds to the wait time, so it should be kept simple.
method()Get the HTTP method (if defined)
HttpRequest$method()(character) the HTTP method
clone()The objects of this class are cloneable with this method.
HttpRequest$clone(deep = FALSE)deepWhether to make a deep clone.
This R6 class doesn't do actual HTTP requests as does
HttpClient() - it is for building requests to use for async HTTP
requests in AsyncVaried()
Note that you can access HTTP verbs after creating an HttpRequest
object, just as you can with HttpClient. See examples for usage.
Also note that when you call HTTP verbs on a HttpRequest object you
don't need to assign the new object to a variable as the new details
you've added are added to the object itself.
See HttpClient() for information on parameters.
http-headers, writing-options
Other async: 
Async,
AsyncQueue,
AsyncVaried
x <- HttpRequest$new(url = "https://hb.opencpu.org/get")
## note here how the HTTP method is shown on the first line to the right
x$get()
## assign to a new object to keep the output
z <- x$get()
### get the HTTP method
z$method()
(x <- HttpRequest$new(url = "https://hb.opencpu.org/get")$get())
x$url
x$payload
(x <- HttpRequest$new(url = "https://hb.opencpu.org/post"))
x$post(body = list(foo = "bar"))
HttpRequest$new(
  url = "https://hb.opencpu.org/get",
  headers = list(
    `Content-Type` = "application/json"
  )
)
# retry
(x <- HttpRequest$new(url = "https://hb.opencpu.org/post"))
x$retry("post", body = list(foo = "bar"))
# mock
x <- HttpRequest$new(url = "https://hb.opencpu.org/get")
fun <- function() {
  HttpResponse$new(method = "GET", url = "http://google.com",
    status_code = 200L)
}
x$get(mock = fun)
## ------------------------------------------------
## Method `HttpRequest$verb`
## ------------------------------------------------
z <- HttpRequest$new(url = "https://hb.opencpu.org/get")
res <- z$verb('get', query = list(hello = "world"))
res$payload
Run the code above in your browser using DataLab