How can I do other things with APIs?

️✅ Learning objectives

TODO: This is a new chapter focused on methods, body, etc. Figure out which LOs go here, exactly.

  • POST
  • Etc
library(httr2)

First LO.

What are the pieces of an HTTP request?

HTTP request = URL + headers + method + body

What are HTTP request methods?

  • Paths are nouns
  • Methods are verbs
    • GET (fetch existing thing)
    • POST (push new thing)
    • DELETE (delete existing thing)
    • PATCH (change existing thing)
    • PUT (replace existing thing)
    • CONNECT, OPTIONS, HEAD, TRACE (very rare)

req_method()

  • GET = default without body
  • POST = default with body
  • req_method() to specify (rarely needed)

req_template()

Build request with variables

req_candidates2 <- req_fec |> 
  req_template("GET /{endpoint}", endpoint = "candidates")
identical(req_candidates$url, req_candidates2$url)

A more useful example:

req_fec |> 
  req_template("GET /candidate/{candidate_id}/", candidate_id = "P40014052")

What are the pieces of an HTTP request?

HTTP request = URL + headers + method + body