-2

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

  • What part are you having trouble with? What have you tried? Generally speaking, the `httr` package is good for working with API's. Beyond that, please provide more details and a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of your issue. – Marcus May 04 '20 at 05:44

1 Answers1

0

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