我正在尝试使用 httr 和来自rapidapi.com的代码片段来使用天空扫描仪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
这更正了 rapid API 片段上的确切代码。
始终使用 RapidAPI 提供的代码片段。它是真实的并且始终有效。它们通过 40 个不同的库提供对 20 种编程语言的支持。
尝试这个代码片段:
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")