0

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

user4933
  • 1,485
  • 3
  • 24
  • 42

3 Answers3

1

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

kana
  • 605
  • 7
  • 12
1

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.

user4933
  • 1,485
  • 3
  • 24
  • 42
0

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")
patrickmdnet
  • 3,332
  • 1
  • 29
  • 34
Pratham
  • 497
  • 3
  • 7
  • If you look closely there are 3 errors there. an extra ` , "x_rapidapi-key" needs to have an underscore like "x_rapidapi_key" and x_rapidapi-host like "x_rapidapi_host". This is a slight error that you have to fix manually. The code you have there won't work. – user4933 Sep 15 '21 at 09:49