Simple GET

##  return what is sent
library(httr2)
req  <- request("https://httpbin.org")
req

##  Dry run
```r
req   |> req_dry_run()

Run it

resp  <- req |> req_perform()
resp

Body

resp |> resp_body_string()
resp |> resp_body_html()

Add header

req |> 
    req_headers(name =  "jim", location =  quote(eugene) ) |>
    req_headers("ACCEPT" = "application/json") |>
    req_dry_run()

Run it with header

req  <- request("https://httpbin.org/get")
{
resp  <- req |> 
    req_headers(name =  "jim", location =  quote(eugene) ) |>
    req_headers(ACCEPT = "application/json") |>
    req_perform()
}
resp |> resp_body_json()
## SAME as
##  why need get?
req <- request("https://httpbin.org/get")
req

Add query string

## tack on query
req2 <- request("https://httpbin.org/get?q=joe")
req2
## Examine, headers automatically added
req %>% req_dry_run()

Add a body, in json

req |>
    req_body_json(list(x=1, friend="joe")) |>
    req_dry_run()

Run request

## fills a buffer, see help for example.
if (F) req |> req_stream()

##  To Console
    req |> req_perform()

##  Capture response
    resp  <- req |> req_perform()
    resp

Many verbs to extract specific resp pieces

resp_body_raw(resp)
resp_body_json(resp)

##  See help, must add code for these to work 
    if(F)   resp_body_xml(resp)
    if(F)   resp_body_xml(resp, check_type=F )
    if(F)   resp_body_html(resp)

resp_content_type(resp)
resp_encoding(resp)
resp_date(resp)

##  HTTP response code
    resp_status(resp)

##  if ERROR, convert to R
    req |> req_url_path_append("abc") |> req_dry_run()

    resp  <- req |> req_url_path_append("abc") |> req_perform()

##  don't undersand
    resp_is_error(resp)
    print(resp_check_status(resp))
    last_response()

Curl

    str  <- "curl http://httpd.org"
    curl_translate(str)

    curl_translate("curl http://example.com --header A:1 --header B:2")

##  curl usage -- very abbreviated
    curl_help()


jimrothstein/yt_api documentation built on Nov. 5, 2022, 8:05 p.m.