Learn R Programming

crul (version 0.3.4)

HttpRequest: HTTP request object

Description

HTTP request object

Arguments

url
(character) A url. One of url or handle required.
opts
(list) curl options, a named list. See curl_options for available curl options
proxies
an object of class proxy, as returned from the proxy function. Supports one proxy for now
headers
(list) a named list of headers
handle
A handle, see handle

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.

Methods

get(path, query, disk, stream, ...)
Define a GET request
post(path, query, body, disk, stream, ...)
Define a POST request
put(path, query, body, disk, stream, ...)
Define a PUT request
patch(path, query, body, disk, stream, ...)
Define a PATCH request
delete(path, query, body, disk, stream, ...)
Define a DELETE request
head(path, disk, stream, ...)
Define a HEAD request
method()
Get the HTTP method (if defined) - returns character string

See HttpClient() for information on parameters.

See Also

post-requests, delete-requests, http-headers, writing-options

Examples

Run this code
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"
  )
)

Run the code above in your browser using DataLab