R + fromJSON - 如何发送标题信息? [英] R + fromJSON - how to send headers info?

查看:94
本文介绍了R + fromJSON - 如何发送标题信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 fromJSON 发送标题信息?

How can I use fromJSON to send header info?

例如,这就是我要求的方式来自服务器的json数据,但服务器将需要我方的一些身份验证信息。

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)

是否可以使用 fromJSON 或其他套餐?

Is it possible with fromJSON or other packages?

推荐答案

如果您想在请求中包含额外的http标头,
您应该使用不同的方法来获取网址内容,
并在响应中使用fromJSON。

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.

在Bing Web Search API上使用httr包的示例:

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

这篇关于R + fromJSON - 如何发送标题信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆