Run
Run

Reputation: 57176

R + fromJSON - how to send headers info?

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?

Upvotes: 2

Views: 3107

Answers (1)

Y.L
Y.L

Reputation: 764

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

Upvotes: 9

Related Questions