crul (version 0.9.0)

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

auth

result of a call to the auth function, e.g. auth(user = "foo", pwd = "bar")

headers

(list) a named list of headers

handle

A handle, see handle

progress

a function with logic for printing a progress bar for an HTTP request, ultimately passed down to curl. only supports httr::progress() for now

hooks

(list) a named list (accepts: request, response) of functions (callbacks) to run on request and response objects. See hooks for more details.

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, ...)

Define a HEAD request

verb(verb, ...)

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

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
# 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"
  )
)

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

Run the code above in your browser using DataCamp Workspace