library(httr)
library(jsonlite)
fetch_endpoint <- function(server, request, content_type){
r <- GET(paste(server, request, sep = ""), accept(content_type))
stop_for_status(r)
if (content_type == 'application/json'){
return (fromJSON(content(r, "text", encoding = "UTF-8")))
} else {
return (content(r, "text", encoding = "UTF-8"))
}
}
# define the gene name
gene_name <- "ESPN"
# define the general URL parameters
server <- "http://rest.ensembl.org/"
con <- "application/json"
# define REST query to get the gene ID from the gene name
ext_get_lookup <- paste("lookup/symbol/homo_sapiens/", gene_name, "?", sep ="")
get_lookup <- fetch_endpoint(server, ext_get_lookup, con)
prettify(toJSON(get_lookup))