Open In App

Accessing REST API using R Programming

Improve
Improve
Like Article
Like
Save
Share
Report

REST(Representational state transfer) API is an architectural style that includes specific constraints for building APIs to ensure that they are consistent, efficient, and scalable. REST API is a collection of syntax and constraints which are used in the development and operation of web services that include sending & receiving information through their endpoint i.e a URL providing an interface to the external environment. 

REST was first presented by Roy Fielding in 2000. The abstraction of information in REST is termed as a resource. REST uses a resource identifier to identify the particular resource in an interaction between components. It allows an application to access resources or functionality available on another server that is remote to that application’s architectural and security domain.

Accessing REST API using R Programming

REST uses resource methods to perform the desired transition. An API can only be considered RESTful when it meets the following conditions:

  • Uniform Interface: A well-defined interface between server and client.
  • Stateless: A state is managed via the requests themselves and not through the support of an external service.
  • Cacheable: Responses should be cacheable in order to improve scalability.
  • Client-Server: Well-defined separation of client and server.
  • Layered System: The client should be unaware of intermediaries between the client and the server.
  • Code on Demand: Response can include logic executable by the client

Accessing REST API using R Programming

The 4 most important HTTP protocol methods;

  1. GET: Retrieves data from a remote server and can be a single resource or a list of resources.
  2. POST: Creates a new resource on a remote server. *
  3. PUT: Updates the data on a remote server.
  4. DELETE: Deletes data from a remote server.

REST API can be used with any language as the requests are based on the universal HTTP protocol. In R,  we use the httr package and jsonlite package to make a GET request, parse the results, and finally page through all the data. This requires converting the raw data from the GET request to JSON format and then into a parsed data frame.

Implementation in R

The API

The API if of movie Guardians of Galaxy Vol. 2 was released in the year 2017 on 05th May of runtime 136 minutes directed by James Gun. It was Action, Comedy, Adventure, and Sci-fi movie. The API is of OMDb which is an open web service that hosts movie information. 

R




# Installing the packages
install.packages("httr")
install.packages("jsonlite")
 
# Loading packages
library(httr)
library(jsonlite)
 
# Initializing API Call


 Accessing movie API using packages

Accessing the movie API using the httr package and jsonlite package in R.

R




# Getting details in API
get_movie_details <- GET(url = call)
 
# Getting status of HTTP Call
status_code(get_movie_details)
 
# Content in the API
str(content(get_movie_details))
 
# Converting content to text
get_movie_text <- content(get_movie_details,
                          "text", encoding = "UTF-8")
get_movie_text
 
# Parsing data in JSON
get_movie_json <- fromJSON(get_movie_text,
                           flatten = TRUE)
get_movie_json
 
# Converting into dataframe
get_movie_dataframe <- as.data.frame(get_movie_json)


Output:

  • Status_code:

Status_code

Status code 200 shows that data of API is successfully requested, responded, and received.

  • Content in the API:

The requested API data is displayed using the content() function.

  • get_movie_text:

The data in API is converted into text format.

  • get_movie_json:

The data in API is parsed into JSON format.

REST API is used in many technical industries and subdomains like Web service, Artificial Intelligence with its full use.



Last Updated : 28 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads