Learn R Programming

nanonext (version 0.6.0)

ncurl: ncurl

Description

nano cURL - a minimalist http(s) client.

Usage

ncurl(
  url,
  async = FALSE,
  convert = TRUE,
  follow = FALSE,
  method = NULL,
  headers = NULL,
  data = NULL,
  response = NULL,
  pem = NULL
)

Value

Named list of 4 elements:

  • $status - integer HTTP repsonse status code (200 - OK).

  • $headers - named list of response headers supplied in 'response' or NULL if unspecified.

  • $raw - raw vector of the received resource (use writeBin to save to a file).

  • $data - converted character string (if 'convert' = TRUE and content is a recognised text format), or NULL otherwise. Other tools can be used to further parse this as html, json, xml etc. if required.

Or else, if async = TRUE, an 'ncurlAio' (object of class 'ncurlAio' and 'recvAio') (invisibly).

Arguments

url

the URL address.

async

[default FALSE] logical value whether to perform actions async.

convert

[default TRUE] logical value whether to attempt conversion of the received raw bytes to a character vector.

follow

[default FALSE] logical value whether to automatically follow redirects. See 'redirects' section below.

method

(optional) the HTTP method (defaults to 'GET' if not specified).

headers

(optional) a named list or character vector specifying the HTTP request headers e.g. list(`Content-Type` = "text/plain") or c(Authorization = "Bearer APIKEY").

data

(optional) the request data to be submitted.

response

(optional) a character vector or list specifying the response headers to return e.g. c("date", "server") or list("Date", "Server"). These are case-insensitive and will return NULL if not present.

pem

(optional) applicable to secure HTTPS sites only. The path to a file containing X.509 certificate(s) in PEM format, comprising the certificate authority certificate chain (and revocation list if present). If missing or NULL, certificates are not validated.

Redirects

If redirects are not followed (the default), the redirect address is returned as a character string.

For async requests, the redirect address is returned as a character string at $raw and $data will be NULL.

Examples

Run this code
ncurl("https://httpbin.org/get", response = c("date", "server"))
ncurl("http://httpbin.org/put",,,,"PUT", list(Authorization = "Bearer APIKEY"), "hello world")
ncurl("http://httpbin.org/post",,,,"POST", c(`Content-Type` = "application/json"),'{"k":"v"}')

Run the code above in your browser using DataLab