Use RapidAPI's SkyScanner integration using httr

191 views Asked by At

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

3

There are 3 answers

0
user4933 On BEST ANSWER

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.

1
piie On

I'm not sure if this is the right response for this, but your first code snippet has the extra ' at the end like you said:

config = httr::add_headers(x_rapidapi-key = API_KEY , x_rapidapi-host = HOST_URL,'),

try changing to

config = httr::add_headers(x_rapidapi-key = API_KEY , x_rapidapi-host = HOST_URL),

Altogether I'd try :

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, content_type("application/octet-stream")))

Then check the response by just response

1
Pratham On

Always use the code snippet that RapidAPI provides. It's authentic and always works. They provide support for 20 programming languages with 40 different libraries.

Try this code snippet:

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