forked from ropensci-training/user2016-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-collecting-data-from-apis.R
66 lines (48 loc) · 1.3 KB
/
01-collecting-data-from-apis.R
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
## indicative live code if following along with the slides
## 01-collecting-data-from-apis.pdf
library(httr)
x <- GET('https://google.com/')
str(x)
class(x)
names(x)
x$headers
x$status_code
x$content
GET("https://httpbin.org/get")
POST("https://httpbin.org/post")
x <- POST("https://httpbin.org/get")
x$status_code
x <- GET("https://httpbin.org/post")
x$status_code
url <- "https://httpbin.org/"
# Request with query parameters
x <- GET(url, query = list(a = 5))
x$url
# Request with headers
x <- GET(url, add_headers(wave = "hi"))
x$request$headers
# Request with a body
x <- POST("https://httpbin.org/post", body = list(a = 5))
x <- POST(modify_url(url, path = "post"), body = list(a = 5))
x$url
x$request$fields
x <- GET('https://google.com/')
x$content
content(x)
res <- GET("http://httpbin.org/get")
http_status(res)
res$request$headers
headers(res)
res$headers$`content-type`
# change accept content type
res <- GET("http://httpbin.org/get", accept_json())
res400 <- GET("http://httpbin.org/status/400")
res500 <- GET("http://httpbin.org/status/500")
content(res400)
content(res500)
j1 <- GET("http://www.omdbapi.com/?t=iron%20man%202&r=json")
content(j1, as = "text")
content(j1, as = "parsed")
x1 <- GET("http://www.omdbapi.com/?t=iron%20man%202&r=xml")
content(x1, as = "text")
content(x1, as = "parsed")