0

I'm trying to get the API R code of RapidAPI to work.

The code they provide is as follows (schematically):

library(httr)

GET(
  url = rapidapi_url,
  config = list(x-rapidapi-key = API_KEY,
                x-rapidapi-host = HOST_URL,
                useQueryString = TRUE),
  inboundpartialdate = "2019-12-01"
)

This code doesn't work because (to start) the variables contain a minus.

I changed the code in the following format

#Define Constants
x_rapidapi_key <- xyz # defined higher up in the script for confidentiality reasons and works in rapidapi.com
HTML_link <- "https://yh-finance.p.rapidapi.com/stock/v3/get-historical-data"
YH_file_type = "json"

#' Paste the get_text together
get_text <- paste(HTML_link,
                  "?q=TTF",
                  "&x-rapidapi-host:",x_rapidapi_host,
                  "&x-rapidapi-key:",x_rapidapi_key,
                  "&out=",YH_file_type,sep="")
httr::GET(get_text)

When running this script I get a 401 error although when I test the api-key in RapidAPI everything works fine.

Response [https://yh-finance.p.rapidapi.com/stock/v3/get-historical-data?q=TTF&x-rapidapi-host:yh-finance.p.rapidapi.com&x-rapidapi-key:xyz&out=json]
  Date: 2021-11-08 14:16
  Status: 401
  Content-Type: application/json
  Size: 91 B

I edited the api-key in the response.

See below another variation for the same thing:


library(httr)

url <- "https://yh-finance.p.rapidapi.com/stock/v3/get-historical-data"

queryString <- list(
  symbol = "TTF=F",
  region = "US"
)

response <- VERB("GET",
                 url,
                 add_headers(x_rapidapi_host = 'yh-finance.p.rapidapi.com',
                             x_rapidapi_key = 'xyz'),
                 query = queryString,
                 content_type("application/octet-stream"))
response

content(response, "text")

Does anybody know how to make this work?

Thanks for the help,

Michael

Michael
  • 59
  • 7

1 Answers1

0

For the record: problem solved:

library(httr)

queryString <- list(
  symbol = "TTF=F",
  region = "US"
)

res <- GET("https://yh-finance.p.rapidapi.com/stock/v3/get-historical-data",
    add_headers(`x-rapidapi-host`='yh-finance.p.rapidapi.com',
                `x-rapidapi-key`= '[your API Key]'),
    query = queryString)


data <- jsonlite::fromJSON(rawToChar(res$content))

The secret seems to be a combination of the right ` and '. Not sure why this makes such a big difference, but it works.

Regards, Michael

Michael
  • 59
  • 7