What's the equivalent of httr::content(api_response, type="text/csv") in the new R package httr2?

294 Views Asked by At

I'm trying to update my code to pull data from an API. I'm using currently the httr package, but it has been deprecated, so I want to replace my code using the httr2 package. I'm trying to download data in a data frame format. The code using httr is:

api_response <- httr::GET(url, httr::authenticate(user_name, password))
data <- httr::content(api_response, type="text/csv")

data is a data frame. How do I get the same result using httr2?

I've tried:

api_response <- request(url) %>% 
    req_auth_basic(user_name,
                   password) %>%
    req_perform()

and I get a httr2_response object and no errors. I don't know how to perform the next step of downloading the data in a data frame format.

1

There are 1 best solutions below

3
MrFlick On

As already stated in an httr2 github issue, you want to extract the contents as a string and parse it yourself explicitly with readr::read_csv

api_response <- request(url) |>
    req_auth_basic(user_name, password) |>
    req_perform() |>
    resp_body_string() |>
    readr::read_csv()

This was basically what httr was doing under the hood