- Import all required libraries
# This package is required for Accessing APIS (HTTP or HTTPS URLS from Web)
library(httr)
#This package exposes some additional functions to convert json/text to data frame
library(rlist)
#This package exposes some additional functions to convert json/text to data frame
library(jsonlite)
#This library is used to manipulate data
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
resp<-GET("https://reqres.in/api/users?pageid=2")
#.When we get the response from API we will use to very basic methods of httr.
http_type(resp) #. This method will tell us what is the type of response fetched from GET() call to the API.
## [1] "application/json"
http_error(resp) #. This method just verifies if the response is error free for processing
## [1] FALSE
query<-list(page="2")
resp<-GET("https://reqres.in/api/users",query=query)
http_type(resp)
## [1] "application/json"
http_error(resp)
## [1] FALSE
# Shows raw data which is not structured and readable
jsonRespText<-content(resp,as="text")
jsonRespText
## [1] "{\"page\":2,\"per_page\":3,\"total\":12,\"total_pages\":4,\"data\":[{\"id\":4,\"first_name\":\"Eve\",\"last_name\":\"Holt\",\"avatar\":\"https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg\"},{\"id\":5,\"first_name\":\"Charles\",\"last_name\":\"Morris\",\"avatar\":\"https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg\"},{\"id\":6,\"first_name\":\"Tracey\",\"last_name\":\"Ramos\",\"avatar\":\"https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg\"}]}"
# Structurised data in form of R vectors and lists
jsonRespParsed<-content(resp,as="parsed")
jsonRespParsed
## $page
## [1] 2
##
## $per_page
## [1] 3
##
## $total
## [1] 12
##
## $total_pages
## [1] 4
##
## $data
## $data[[1]]
## $data[[1]]$id
## [1] 4
##
## $data[[1]]$first_name
## [1] "Eve"
##
## $data[[1]]$last_name
## [1] "Holt"
##
## $data[[1]]$avatar
## [1] "https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"
##
##
## $data[[2]]
## $data[[2]]$id
## [1] 5
##
## $data[[2]]$first_name
## [1] "Charles"
##
## $data[[2]]$last_name
## [1] "Morris"
##
## $data[[2]]$avatar
## [1] "https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg"
##
##
## $data[[3]]
## $data[[3]]$id
## [1] 6
##
## $data[[3]]$first_name
## [1] "Tracey"
##
## $data[[3]]$last_name
## [1] "Ramos"
##
## $data[[3]]$avatar
## [1] "https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg"
fromJSON(jsonRespText)
## $page
## [1] 2
##
## $per_page
## [1] 3
##
## $total
## [1] 12
##
## $total_pages
## [1] 4
##
## $data
## id first_name last_name
## 1 4 Eve Holt
## 2 5 Charles Morris
## 3 6 Tracey Ramos
## avatar
## 1 https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg
## 2 https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg
## 3 https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg
modJson<-jsonRespParsed$data #. Access data element of whole list and ignore other vectors
modJson
## [[1]]
## [[1]]$id
## [1] 4
##
## [[1]]$first_name
## [1] "Eve"
##
## [[1]]$last_name
## [1] "Holt"
##
## [[1]]$avatar
## [1] "https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"
##
##
## [[2]]
## [[2]]$id
## [1] 5
##
## [[2]]$first_name
## [1] "Charles"
##
## [[2]]$last_name
## [1] "Morris"
##
## [[2]]$avatar
## [1] "https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg"
##
##
## [[3]]
## [[3]]$id
## [1] 6
##
## [[3]]$first_name
## [1] "Tracey"
##
## [[3]]$last_name
## [1] "Ramos"
##
## [[3]]$avatar
## [1] "https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg"
#Using dplyr and base R
modJson%>%bind_rows%>%select(id,first_name,last_name,avatar)
## # A tibble: 3 x 4
## id first_name last_name avatar
## <int> <chr> <chr> <chr>
## 1 4 Eve Holt https://s3.amazonaws.com/uifaces/faces/twitt~
## 2 5 Charles Morris https://s3.amazonaws.com/uifaces/faces/twitt~
## 3 6 Tracey Ramos https://s3.amazonaws.com/uifaces/faces/twitt~
list.select(modJson,id,first_name)
## [[1]]
## [[1]]$id
## [1] 4
##
## [[1]]$first_name
## [1] "Eve"
##
##
## [[2]]
## [[2]]$id
## [1] 5
##
## [[2]]$first_name
## [1] "Charles"
##
##
## [[3]]
## [[3]]$id
## [1] 6
##
## [[3]]$first_name
## [1] "Tracey"
list.stack(modJson)
## id first_name last_name
## 1 4 Eve Holt
## 2 5 Charles Morris
## 3 6 Tracey Ramos
## avatar
## 1 https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg
## 2 https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg
## 3 https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg
- Results obtained from dplyr and baseR and with rlist pacakges are ver similar.
post_result <- POST(url="http://httpbin.org/post",body="this is a test") # where body argument accpets data we wish to send to server