:-- title: "r knitr::current_input()" date: "r paste('last updated', format(lubridate::now(), '%H:%M, %d %B %Y'))" output: pdf_document: latex_engine: xelatex toc: TRUE toc_depth: 1 fontsize: 12pt geometry: margin=0.5in,top=0.25in TAGS: httr,


PURPOSE: HTTR2:: practice

REF :

===========================================

https://httr2.r-lib.org/articles/httr2.html

===========================================

knitr::opts_chunk$set(echo = TRUE,
                      comment = "      ##",
                      error = TRUE,
                      collapse = TRUE)
library(httr2)
#  [1] "httr2"     "nvimcom"   "stats"     "graphics"  "grDevices" "utils"     "datasets" 
#  [8] "rlang"     "methods"   "base"     
load_all()
library(DiagrammeR)

GET can have header, query, not body

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

##  why need get?
req <- request("https://httpbin.org/get")
req

## tack on query
req2 <- request("https://httpbin.org/get?q=joe")
req2

## Examine, headers automatically added
req %>% req_dry_run()


## Add header  
req |> 
    req_headers(name =  "jim", location =  quote(eugene) ) |>
    req_headers("ACCEPT" = "application/json") |>
    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.