I’m having problems getting the R code from RapidAPI to function properly. The sample code they give me looks like this:
library(httr)
GET(
url = api_endpoint,
config = list(x-rapidapi-key = MY_KEY,
x-rapidapi-host = HOST_NAME,
useQueryString = TRUE),
datefilter = "2020-01-15"
)
This doesn’t work because the variable names have dashes in them which breaks R syntax. I tried to fix it by changing the approach:
# Set up variables
api_key <- "my_secret_key" # actual key defined elsewhere
base_url <- "https://stock-data.p.rapidapi.com/v2/historical-prices"
output_format <- "json"
# Build the request URL
full_url <- paste(base_url,
"?symbol=AAPL",
"&x-rapidapi-host:", api_host,
"&x-rapidapi-key:", api_key,
"&format=", output_format, sep="")
httr::GET(full_url)
But I keep getting 401 unauthorized errors even though my API key works fine when I test it directly on the RapidAPI website. I also tried this alternative method:
library(httr)
endpoint <- "https://stock-data.p.rapidapi.com/v2/historical-prices"
params <- list(
ticker = "AAPL",
market = "NASDAQ"
)
api_response <- VERB("GET",
endpoint,
add_headers(x_rapidapi_host = 'stock-data.p.rapidapi.com',
x_rapidapi_key = 'my_secret_key'),
query = params,
content_type("application/octet-stream"))
print(api_response)
content(api_response, "text")
Still no luck. Anyone know what I’m doing wrong here?