-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_post.R
38 lines (33 loc) · 1.05 KB
/
demo_post.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
if(!require(dplyr)) {
install.packages("dplyr", repos="http://cran.us.r-project.org")
require(dplyr)
}
if(!require(rvest)) {
install.packages("rvest", repos="http://cran.us.r-project.org")
require(rvest)
}
if(!require(httr)) {
install.packages("httr", repos="http://cran.us.r-project.org")
require(httr)
}
if(!require(purrr)) {
install.packages("purrr", repos="http://cran.us.r-project.org")
require(purrr)
}
if(!require(jsonlite)) {
install.packages("jsonlite", repos="http://cran.us.r-project.org")
require(jsonlite)
}
# screen scraping not possible here:
base_url <- 'https://httpbin.org'
# send a raw body without content-type spec
POST(glue::glue('{base_url}/post'), body = 'hello') %>%
content()
# send some json as a string
POST(glue::glue('{base_url}/post'), body = '{"foo": "bar"}',
add_headers('Content-type' = 'application/json')) %>%
content()
# send some json from a list^
POST(glue::glue('{base_url}/post'), body = list('foo' = 'bar'), encode = 'json',
add_headers('Content-type' = 'application/json')) %>%
content()