Learn R Programming

nanonext (version 0.5.1)

ncurl: ncurl

Description

nano cURL - a minimalist http(s) client.

Usage

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

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.

Value

Named list of 2 elements:

  • $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, a 'recvAio' (object of class 'recvAio').

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.

TLS Support

Connecting to secure https sites is supported if nng_version shows 'TLS supported'.

Examples

Run this code
# NOT RUN {
ncurl("http://httpbin.org/get")
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