如何使用 fromJSON
发送 header 信息?
例如,这是我从服务器请求 json 数据的方式,但服务器将需要我这边的一些身份验证信息。
public_key <- 'VzUZFW1cQzP08ovr5auZbXQduzE';
data <- fromJSON(paste('http://127.0.0.1:3000', "/output/data?public_key=", public_key, sep=""),flatten=TRUE)
是否可以使用 fromJSON
或其他包?
最佳答案
如果您想在请求中包含额外的 http header , 你应该使用不同的方法来获取 url 内容, 并在响应中使用 fromJSON。
在 Bing Web 搜索 API 上使用 httr 包的示例:
library(httr)
library(jsonlite)
QUERY = "your search query here..."
API_KEY = "your api key here...."
url = paste0("https://api.cognitive.microsoft.com/bing/v5.0/search?",
"mkt=en-US&setLang=en-US&responseFilter=Webpages&textDecorations=false&textFormat=Raw&q=",
QUERY)
httpResponse <- GET(url, add_headers("Ocp-Apim-Subscription-Key" = API_KEY), accept_json())
results = fromJSON(content(httpResponse, "text"))
https://stackoverflow.com/questions/40289067/