Learn R Programming

crul (version 0.4.0)

HttpClient: 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

headers

(list) a named list of headers

handle

A handle, see handle

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.

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

Make a HEAD request

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

  • path - URL path, appended to the base URL

  • query - query terms, as a list

  • body - body as an R list

  • encode - one of form, multipart, json, or raw

  • 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

See Also

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

Examples

Run this code
# NOT RUN {
(x <- HttpClient$new(url = "https://httpbin.org"))
x$url
(res_get1 <- x$get('get'))
res_get1$content
res_get1$response_headers
res_get1$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'))
# }

Run the code above in your browser using DataLab