Learn R Programming

nanonext (version 0.5.4)

ncurl: ncurl

Description

nano cURL - a minimalist http(s) client.

Usage

ncurl(
  url,
  async = FALSE,
  convert = TRUE,
  method = NULL,
  headers = NULL,
  data = NULL,
  request = 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 'request' 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').

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.

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.

request

(optional) a character vector or list specifying the response headers to request 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

In interactive sessions: will prompt upon receiving a redirect location whether to follow or not (default: yes).

In non-interactive sessions: redirects are never followed.

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

Examples

Run this code
ncurl("https://httpbin.org/get", request = 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