R + fromJSON - how to send headers info?

让人想犯罪 __ 提交于 2019-12-23 01:36:24

问题


How can I use fromJSON to send header info?

For instance, this is how i request a json data from a server, but the server will require some authentication info from my side.

public_key <- 'VzUZFW1cQzP08ovr5auZbXQduzE';
data <- fromJSON(paste('http://127.0.0.1:3000', "/output/data?public_key=", public_key, sep=""),flatten=TRUE)

Is it possible with fromJSON or other packages?


回答1:


If you want to include extra http headers in your request, you should use a different method for getting the url content, and use fromJSON on the response.

Example using the httr package on the Bing Web Search API:

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/r-fromjson-how-to-send-headers-info

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!