Accessing REST API using R Programming - GeeksforGeeks

28 Dec, 2021
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.
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
GET: Retrieves data from a remote server and can be a single resource or a list of resources.POST: Creates a new resource on a remote server. *PUT: Updates the data on a remote server.DELETE: Deletes data from a remote server.
GET: Retrieves data from a remote server and can be a single resource or a list of resources.
POST: Creates a new resource on a remote server. *
PUT: Updates the data on a remote server.
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.
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 packagesinstall.packages("httr")install.packages("jsonlite") # Loading packageslibrary(httr)library(jsonlite) # Initializing API Callcall <- "http://www.omdbapi.com/?i=tt3896198&apikey=948d3551&plot=full&r=json"
Accessing the movie API using the httr package and jsonlite package in R.
R
# Getting details in APIget_movie_details <- GET(url = call) # Getting status of HTTP Callstatus_code(get_movie_details) # Content in the APIstr(content(get_movie_details)) # Converting content to textget_movie_text <- content(get_movie_details,                          "text", encoding = "UTF-8")get_movie_text # Parsing data in JSONget_movie_json <- fromJSON(get_movie_text,                           flatten = TRUE)get_movie_json # Converting into dataframeget_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.
kumar_satyam
R Data-science
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
Logistic Regression in R Programming
How to change the order of bars in bar chart in R ?
How to filter R dataframe by multiple conditions?
Replace Specific Characters in String in R
Data Visualization in R