File: quickstart.R

package info (click to toggle)
r-cran-httr 1.4.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 968 kB
  • sloc: sh: 9; makefile: 2
file content (106 lines) | stat: -rw-r--r-- 3,771 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
## ---- echo = FALSE------------------------------------------------------------
library(httr)
knitr::opts_chunk$set(comment = "#>", collapse = TRUE)

## -----------------------------------------------------------------------------
library(httr)
r <- GET("http://httpbin.org/get")

## -----------------------------------------------------------------------------
r

## -----------------------------------------------------------------------------
status_code(r)
headers(r)
str(content(r))

## -----------------------------------------------------------------------------
r <- GET("http://httpbin.org/get")
# Get an informative description:
http_status(r)

# Or just access the raw code:
r$status_code

## -----------------------------------------------------------------------------
warn_for_status(r)
stop_for_status(r)

## -----------------------------------------------------------------------------
r <- GET("http://httpbin.org/get")
content(r, "text")

## ---- eval = FALSE------------------------------------------------------------
#  content(r, "text", encoding = "ISO-8859-1")

## -----------------------------------------------------------------------------
content(r, "raw")

## ---- eval = FALSE------------------------------------------------------------
#  bin <- content(r, "raw")
#  writeBin(bin, "myfile.txt")

## -----------------------------------------------------------------------------
# JSON automatically parsed into named list
str(content(r, "parsed"))

## -----------------------------------------------------------------------------
headers(r)

## -----------------------------------------------------------------------------
headers(r)$date
headers(r)$DATE

## -----------------------------------------------------------------------------
r <- GET("http://httpbin.org/cookies/set", query = list(a = 1))
cookies(r)

## -----------------------------------------------------------------------------
r <- GET("http://httpbin.org/cookies/set", query = list(b = 1))
cookies(r)

## -----------------------------------------------------------------------------
r <- GET("http://httpbin.org/get", 
  query = list(key1 = "value1", key2 = "value2")
)
content(r)$args

## -----------------------------------------------------------------------------
r <- GET("http://httpbin.org/get", 
  query = list(key1 = "value 1", "key 2" = "value2", key2 = NULL))
content(r)$args

## -----------------------------------------------------------------------------
r <- GET("http://httpbin.org/get", add_headers(Name = "Hadley"))
str(content(r)$headers)

## -----------------------------------------------------------------------------
r <- GET("http://httpbin.org/cookies", set_cookies("MeWant" = "cookies"))
content(r)$cookies

## -----------------------------------------------------------------------------
r <- POST("http://httpbin.org/post", body = list(a = 1, b = 2, c = 3))

## -----------------------------------------------------------------------------
url <- "http://httpbin.org/post"
body <- list(a = 1, b = 2, c = 3)

# Form encoded
r <- POST(url, body = body, encode = "form")
# Multipart encoded
r <- POST(url, body = body, encode = "multipart")
# JSON encoded
r <- POST(url, body = body, encode = "json")

## ---- eval = FALSE------------------------------------------------------------
#  POST(url, body = body, encode = "multipart", verbose()) # the default
#  POST(url, body = body, encode = "form", verbose())
#  POST(url, body = body, encode = "json", verbose())

## ---- eval = FALSE------------------------------------------------------------
#  POST(url, body = upload_file("mypath.txt"))
#  POST(url, body = list(x = upload_file("mypath.txt")))

## -----------------------------------------------------------------------------
sessionInfo()