Home > database >  Can the httr package make HTTPS calls?
Can the httr package make HTTPS calls?

Time:02-14

Code that previously worked now throws an error due to the server requesting HTTPS calls and no longer accepts HTTP.

Can this code me modified to work? Or is a new process required for encrypted HTTPS?

Thank you!

# 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)

CodePudding user response:

Yes, it can - you just use https in the URL.

The shortest demonstration I could think of is:

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

In your code you just need to change the line where you define the url variable:

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