Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

HttpRequest

HTTP request object


Description

Create HTTP requests

Details

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.

Public fields

url

(character) a url

opts

(list) named list of curl options

proxies

a proxy() object

auth

an auth() object

headers

(list) named list of headers, see http-headers

handle

a handle()

progress

only supports httr::progress(), see progress

payload

resulting payload after request

Methods

Public methods


Method print()

print method for HttpRequest objects

Usage
HttpRequest$print(x, ...)
Arguments
x

self

...

ignored


Method new()

Create a new HttpRequest object

Usage
HttpRequest$new(url, opts, proxies, auth, headers, handle, progress)
Arguments
url

(character) A url. One of url or handle required.

opts

any curl options

proxies

a proxy() object

auth

an auth() object

headers

named list of headers, see http-headers

handle

a handle()

progress

only supports httr::progress(), see progress

urls

(character) one or more URLs

Returns

A new HttpRequest object


Method get()

Define a GET request

Usage
HttpRequest$get(path = NULL, query = list(), disk = NULL, stream = NULL, ...)
Arguments
path

URL path, appended to the base URL

query

query terms, as a named list

disk

a path to write to. if NULL (default), memory used. See curl::curl_fetch_disk() for help.

stream

an R function to determine how to stream data. if NULL (default), memory used. See curl::curl_fetch_stream() for help

...

curl options, only those in the acceptable set from curl::curl_options() except the following: httpget, httppost, post, postfields, postfieldsize, and customrequest


Method post()

Define a POST request

Usage
HttpRequest$post(
  path = NULL,
  query = list(),
  body = NULL,
  disk = NULL,
  stream = NULL,
  encode = "multipart",
  ...
)
Arguments
path

URL path, appended to the base URL

query

query terms, as a named list

body

body as an R list

disk

a path to write to. if NULL (default), memory used. See curl::curl_fetch_disk() for help.

stream

an R function to determine how to stream data. if NULL (default), memory used. See curl::curl_fetch_stream() for help

encode

one of form, multipart, json, or raw

...

curl options, only those in the acceptable set from curl::curl_options() except the following: httpget, httppost, post, postfields, postfieldsize, and customrequest


Method put()

Define a PUT request

Usage
HttpRequest$put(
  path = NULL,
  query = list(),
  body = NULL,
  disk = NULL,
  stream = NULL,
  encode = "multipart",
  ...
)
Arguments
path

URL path, appended to the base URL

query

query terms, as a named list

body

body as an R list

disk

a path to write to. if NULL (default), memory used. See curl::curl_fetch_disk() for help.

stream

an R function to determine how to stream data. if NULL (default), memory used. See curl::curl_fetch_stream() for help

encode

one of form, multipart, json, or raw

...

curl options, only those in the acceptable set from curl::curl_options() except the following: httpget, httppost, post, postfields, postfieldsize, and customrequest


Method patch()

Define a PATCH request

Usage
HttpRequest$patch(
  path = NULL,
  query = list(),
  body = NULL,
  disk = NULL,
  stream = NULL,
  encode = "multipart",
  ...
)
Arguments
path

URL path, appended to the base URL

query

query terms, as a named list

body

body as an R list

disk

a path to write to. if NULL (default), memory used. See curl::curl_fetch_disk() for help.

stream

an R function to determine how to stream data. if NULL (default), memory used. See curl::curl_fetch_stream() for help

encode

one of form, multipart, json, or raw

...

curl options, only those in the acceptable set from curl::curl_options() except the following: httpget, httppost, post, postfields, postfieldsize, and customrequest


Method delete()

Define a DELETE request

Usage
HttpRequest$delete(
  path = NULL,
  query = list(),
  body = NULL,
  disk = NULL,
  stream = NULL,
  encode = "multipart",
  ...
)
Arguments
path

URL path, appended to the base URL

query

query terms, as a named list

body

body as an R list

disk

a path to write to. if NULL (default), memory used. See curl::curl_fetch_disk() for help.

stream

an R function to determine how to stream data. if NULL (default), memory used. See curl::curl_fetch_stream() for help

encode

one of form, multipart, json, or raw

...

curl options, only those in the acceptable set from curl::curl_options() except the following: httpget, httppost, post, postfields, postfieldsize, and customrequest


Method head()

Define a HEAD request

Usage
HttpRequest$head(path = NULL, ...)
Arguments
path

URL path, appended to the base URL

...

curl options, only those in the acceptable set from curl::curl_options() except the following: httpget, httppost, post, postfields, postfieldsize, and customrequest


Method verb()

Use an arbitrary HTTP verb supported on this class Supported verbs: get, post, put, patch, delete, head

Usage
HttpRequest$verb(verb, ...)
Arguments
verb

an 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

Examples
z <- HttpRequest$new(url = "https://httpbin.org/get")
res <- z$verb('get', query = list(hello = "world"))
res$payload

Method method()

Get the HTTP method (if defined)

Usage
HttpRequest$method()
Returns

(character) the HTTP method


Method clone()

The objects of this class are cloneable with this method.

Usage
HttpRequest$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

See Also

Other async: AsyncQueue, AsyncVaried, Async

Examples

## Not run: 
x <- HttpRequest$new(url = "https://httpbin.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://httpbin.org/get")$get())
x$url
x$payload

(x <- HttpRequest$new(url = "https://httpbin.org/post"))
x$post(body = list(foo = "bar"))

HttpRequest$new(
  url = "https://httpbin.org/get",
  headers = list(
    `Content-Type` = "application/json"
  )
)

## End(Not run)

## ------------------------------------------------
## Method `HttpRequest$verb`
## ------------------------------------------------

z <- HttpRequest$new(url = "https://httpbin.org/get")
res <- z$verb('get', query = list(hello = "world"))
res$payload

crul

HTTP Client

v1.1.0
MIT + file LICENSE
Authors
Scott Chamberlain [aut, cre] (<https://orcid.org/0000-0003-1444-9135>)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.