How to Make an HTTP Request in R with httr2

· 1 min read · Updated March 15, 2026 · beginner
r http api httr2 web

The httr2 package is the modern way to make HTTP requests in R. It provides a pipe-friendly interface for working with REST APIs.

Basic GET Request

library(httr2)
req <- request("https://httpbin.org/get")
resp <- req_perform(req)
resp_body_string(resp)

Adding Query Parameters

req <- request("https://httpbin.org/get") |>
  req_url_query(name = "Alice", age = 30)
resp <- req_perform(req)

POST Request with JSON

req <- request("https://httpbin.org/post") |>
  req_method("POST") |>
  req_body_json(list(user = "alice", active = TRUE))
resp <- req_perform(req)

Setting Headers

req <- request("https://httpbin.org/headers") |>
  req_headers("Authorization" = "Bearer mytoken")
resp <- req_perform(resp)

Parsing Responses

# Parse JSON response
resp |> resp_body_json()

# Check status code
resp_status(resp)

See Also