HttpClient {crul}R Documentation

HTTP client

Description

HTTP client

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, ultimiately passed down to curl. only supports httr::progress() for now

Details

Methods

get(path, query, disk, stream, ...)

Make a GET request

post(path, query, body, disk, stream, ...)

Make a POST request

put(path, query, body, disk, stream, ...)

Make a PUT request

patch(path, query, body, disk, stream, ...)

Make a PATCH request

delete(path, query, body, disk, stream, ...)

Make a DELETE request

head(path, query, ...)

Make a HEAD request

Possible parameters (not all are allowed in each HTTP verb):

handles

curl handles are re-used on the level of the connection object, that is, each HttpClient object is separate from one another so as to better separate connections.

If you don't pass in a curl handle to the handle parameter, it gets created when a HTTP verb is called. Thus, if you try to get handle after creating a HttpClient object only passing url parameter, handle will be NULL. If you pass a curl handle to the handle parameter, then you can get the handle from theHttpClientobject. The response from a http verb request does have the handle in thehandle' slot.

Note

a little quark about crul is that because user agent string can be passed as either a header or a curl option (both lead to a User-Agent header being passed in the HTTP request), we return the user agent string in the request_headers list of the response even if you pass in a useragent string as a curl option. Note that whether you pass in as a header like User-Agent or as a curl option like useragent, it is returned as request_headers$User-Agent so at least accessing it in the request headers is consistent.

See Also

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

Examples

## Not run: 
# set your own handle 
(h <- handle("https://httpbin.org"))
(x <- HttpClient$new(handle = h))
x$handle
x$url
(out <- x$get("get"))
x$handle
x$url
class(out)
out$handle

# if you just pass a url, we create a handle for you
#  this is how most people will use HttpClient
(x <- HttpClient$new(url = "https://httpbin.org"))
x$url
x$handle # is empty, it gets created when a HTTP verb is called
(r1 <- x$get('get'))
x$url
x$handle 
r1$url
r1$handle
r1$content
r1$response_headers
r1$parse()

(res_get2 <- x$get('get', query = list(hello = "world")))
res_get2$parse()
library("jsonlite")
jsonlite::fromJSON(res_get2$parse())

# post request
(res_post <- x$post('post', body = list(hello = "world")))

## empty body request
x$post('post')

# put request
(res_put <- x$put('put'))

# delete request
(res_delete <- x$delete('delete'))

# patch request
(res_patch <- x$patch('patch'))

# head request
(res_head <- x$head())

# query params are URL encoded for you, so DO NOT do it yourself
## if you url encode yourself, it gets double encoded, and that's bad
(x <- HttpClient$new(url = "https://httpbin.org"))
res <- x$get("get", query = list(a = 'hello world'))

## End(Not run)

[Package crul version 0.6.0 Index]