I want an R code to connect with this public API -- http://www.omdbapi.com
And the API key is as follows: http://www.omdbapi.com/?i=tt3896198&apikey=4de9f5a6
Request you to help me out with how to connect to the API
I want an R code to connect with this public API -- http://www.omdbapi.com
And the API key is as follows: http://www.omdbapi.com/?i=tt3896198&apikey=4de9f5a6
Request you to help me out with how to connect to the API
1 -- Intall packages
install.packages('jsonlite')
2 -- Require the packages so that we can use it.
require('httr')
require('jsonlite')
Installing the httr package to make GET requests and the jsonlite package to parse the JSON responses
3 -- Make a GET request in R
res= GET('http://www.omdbapi.com/?i=tt3896198&apikey=4de9f5a6')
res
As in the console we see that the status being shown as 200. That means we have a successful response from the API. i.e, we have the data on hand and we can start working on it.
4 -- The actual data is contained as raw Unicode in the res list, which ultimately needs to be converted into JSON format. The rawToChar() function performs just this task, as shown below:
rawToChar(res$content)
5 -- From a character vector, we can convert it into list data structure using the fromJSON() function from the jsonlite library. The fromJSON() function needs a character vector that contains the JSON structure, which is what we got from the rawToChar() output. So, if we string these two functions together, we'll get the data we want in a format that we can more easily manipulate in R.
data= fromJSON(rawToChar(res$content))
names(data)
data$Title
This is how we connect to a public API using R