我正在尝试使用httr和rapidapi.com中的代码片段来使用sky scanner API。这是我第一次尝试这样做。
我的问题是,直接从网站复制的代码不起作用,这是因为代码中有'
。
如何调试此错误以便使用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")
第1版
我在这里的网站上发现了一篇帖子,解释说该网站在代码片段中给出了2个错误,并建议修改代码。然而,这给出了一个不同的错误。我无法正确输入response
对象。
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")
我不确定这是否是正确的响应,但您的第一个代码片段在末尾有额外的",就像您所说的:
config = httr::add_headers(x_rapidapi-key = API_KEY , x_rapidapi-host = HOST_URL,'),
尝试更改为
config = httr::add_headers(x_rapidapi-key = API_KEY , x_rapidapi-host = HOST_URL),
总之,我会尝试:
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")))
然后仅通过response
检查响应
我找到了这个问题的解决方案。这应该会更正代码片段,并让它在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
这纠正了快速API代码段中的确切代码。
始终使用RapidAPI提供的代码片段。它是真实的,而且总是有效的。它们支持20种编程语言和40个不同的库。
试试这个代码片段:
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")