httr 包可以进行 HTTPS 调用吗?

Can the httr package make HTTPS calls?

由于服务器请求 HTTPS 调用并且不再接受 HTTP,之前可以运行的代码现在会抛出错误。

我可以修改此代码以使其工作吗?还是加密 HTTPS 需要新的流程?

谢谢!

# Connecting to EIA API

# install.packages(c("httr", "jsonlite"))

library(httr)
library(jsonlite)

key <- "e77e9bd3c8bc84927fad13088f4bff28"

padd_key <- list('PET.MCRRIP12.M','PET.MCRRIP22.M',
                 'PET.MCRRIP32.M','PET.MCRRIP42.M',
                 'PET.MCRRIP52.M')
startdate <- "2010-01-01" #YYYY-MM-DD
enddate <- "2022-02-13" #YYYY-MM-DD

j = 0
for (i in padd_key) {
  
  url <- paste('http://api.eia.gov/series/?api_key=',key,'&series_id=',i,sep="")
  
  res <- GET(url)
  
  json_data <- fromJSON(rawToChar(res$content))
  data <- data.frame(json_data$series$data)
  data$Year <- substr(data$X1,1,4)
  data$Month <- substr(data$X1,5,6)
  data$Day <- 1
  
  data$Date <- as.Date(paste(data$Year, data$Month, data$Day, sep='-'))
  colnames(data)[2]  <- json_data$series$name
  data <- data[-c(1,3,4,5)]
  
  if (j == 0){
    data_final <- data
  }
  else{
    data_final <- merge(data_final,data,by="Date")
  }
  
  j = j + 1
}

data_final <- subset(data_final, Date >= startdate & Date <= enddate)

是的,可以 - 您只需在 URL 中使用 https。

我能想到的最短的演示是:

httr::GET("https://httpbin.org/get")

在您的代码中,您只需更改定义 url 变量的行:

url <- paste('https://api.eia.gov/series/?api_key=',key,'&series_id=',i,sep="")