Reputation: 25
I am currently retrieving some numeric data from an api of a device. The code runs perfectly without errors however NOW i want to fetch data from multiple devices in a single code i.e multiple api calls if i am not wrong. These would be more than 50 calls since there are 50 devices.
I am running the following code curently for one device :
url <-"https://example.com/api/GetDataBynum?num=ABCDevice.device#.parameter"
geturl <-httr::GET(url,add_headers(.headers=c('key'='')))
apidetails1<-content(geturl,"text", encoding ="UTF-8")
#and this one for second device number and so on which is inefficient
url2 <-"https://example.com/api/GetDataBynum?num=ABCDevice.device#.parameter"
geturl2 <-httr::GET(url2,add_headers(.headers=c('key'='')))
apidetails2<-content(geturl2,"text", encoding ="UTF-8")
Is there any way to run multiple apis in single code ?
Upvotes: 0
Views: 611
Reputation: 389135
Create a vector of url's and use it in lapply
:
library(httr)
url <- sprintf('https://example.com/api/GetDataBynum?num=ABCDevice.device%s.parameter', data$dataf)
lapply(urls, function(x) {
geturl <- GET(x,add_headers(.headers=c('key'='')))
content(geturl,"text", encoding ="UTF-8")
}) -> result
If you want to combine the data into one dataframe you can do result <- do.call(rbind, result)
.
Upvotes: 1