File: crul.Rmd.og

package info (click to toggle)
r-cran-crul 1.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,620 kB
  • sloc: sh: 13; makefile: 2
file content (174 lines) | stat: -rw-r--r-- 2,919 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
---
title: 1. crul introduction
author: Scott Chamberlain
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
    %\VignetteIndexEntry{1. crul introduction}
    %\VignetteEngine{knitr::rmarkdown}
    %\VignetteEncoding{UTF-8}
---

```{r echo=FALSE}
knitr::opts_chunk$set(
  comment = "#>",
  collapse = TRUE,
  warning = FALSE,
  message = FALSE
)
```

`crul` is an HTTP client for R.

## Install

Stable CRAN version

```{r eval=FALSE}
install.packages("crul")
```

Dev version

```{r eval=FALSE}
remotes::install_github("ropensci/crul")
```

```{r}
library("crul")
```

## HttpClient - the main interface

`HttpClient` is where to start

```{r}
(x <- HttpClient$new(
  url = "https://httpbin.org",
  opts = list(
    timeout = 1
  ),
  headers = list(
    a = "hello world"
  )
))
```

Makes a R6 class, that has all the bits and bobs you'd expect for doing HTTP
requests. When it prints, it gives any defaults you've set. As you update
the object you can see what's been set

```{r}
x$opts
```

```{r}
x$headers
```

## Do some HTTP requests

The client object created above has http methods that you can call,
and pass paths to, as well as query parameters, body values, and any other
curl options.

Here, we'll do a __GET__ request on the route `/get` on our base url
`https://httpbin.org` (the full url is then `https://httpbin.org/get`)

```{r}
res <- x$get("get")
```

The response from a http request is another R6 class `HttpResponse`, which
has slots for the outputs of the request, and some functions to deal with
the response:

Status code

```{r}
res$status_code
```

The content

```{r}
res$content
```

HTTP method

```{r}
res$method
```

Request headers

```{r}
res$request_headers
```

Response headers

```{r}
res$response_headers
```

All response headers, including intermediate headers, if any

```{r}
res$response_headers_all
```

And you can parse the content with a provided function:

```{r}
res$parse()
jsonlite::fromJSON(res$parse())
```

With the `HttpClient` object, which holds any configuration stuff
we set, we can make other HTTP verb requests. For example, a `HEAD`
request:

```{r eval=FALSE}
x$post(
  path = "post", 
  body = list(hello = "world")
)
```


## write to disk

```{r}
x <- HttpClient$new(url = "https://httpbin.org")
f <- tempfile()
res <- x$get(disk = f)
# when using write to disk, content is a path
res$content 
```

Read lines

```{r}
readLines(res$content, n = 10)
```

## stream data

```{r}
(x <- HttpClient$new(url = "https://httpbin.org"))
res <- x$get('stream/5', stream = function(x) cat(rawToChar(x)))
# when streaming, content is NULL
res$content 
```

## Learn more 

Learn more with the other vignettes:

- [crul workflows](how-to-use-crul.html)
- [async with crul](async.html)
- [curl options](curl-options.html)
- [API package best practices](best-practices-api-packages.html)
- [Choosing a HTTP request class](choosing-a-client.html)