R + fromJSON - how to send header information?

How can I use fromJSON

to send header information?

For example, this is how I request json data from the server, but the server will ask me for some authentication information.

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

      

Is this possible with fromJSON

or other packages?

0


source to share


1 answer


If you want to include additional optional HTTP headers in your request, you must use a different method to get the URL content, and use the JSON response.

An example of using the httr package in 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"))

      

+2


source







All Articles