Search code examples
rhttrrapidapiskyscanner

Use RapidAPI's SkyScanner integration using httr


I am trying to use httr and the code snippet from rapidapi.com to use sky scanner API.This is the first time I am trying this.

My issue is that the code copied directly from the site is not working and this is because of a ' in the code.

How can I debug this error so that I can use the API?

library(httr)

url0 <- "https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/reference/v1.0/currencies"
API_KEY <- 'my_API_key'
HOST_URL <- 'skyscanner-skyscanner-flight-search-v1.p.rapidapi.com'
response <- VERB(verb="GET",
                  url=url0, 
                 config = httr::add_headers(x_rapidapi-key = API_KEY , x_rapidapi-host = HOST_URL,'),
                 encode = content_type("application/octet-stream"))

content(response, "text")

Edit-1

I found a post on the site here that explained that the site gives 2 errors in the code snippet and suggests altering the code. This is giving a different error however. I cannot correctly input the response object.

library(httr)

url <- "https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/reference/v1.0/currencies"
API_KEY <-  'my_API_key'

response <- VERB("GET",
                 url,
                 add_headers(x-rapidapi-key = API_KEY,
                             x-rapidapi-host = "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com"),
                 content_type("application/octet-stream"))

content(response,"text")


Solution

  • I got a solution to the problem. This should correct the code snippet and let it run in R.

    # Correct
    library(httr)
    
    url <- "https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/reference/v1.0/currencies"
    API_KEY <- "your_key"
    
    response <- VERB("GET",
                     url,
                     add_headers("x-rapidapi-key" = API_KEY,
                                 "x-rapidapi-host" = "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com"),
                     content_type("application/octet-stream"))
    
    content(response,"text")
    
    Robject <- content(response, "text")
    Robject
    

    This corrects the exact code on rapid API's snippet.