2

I need to call an API within R which will make the request to a database server and fetch the results into an R dataframe.

The API I have is of the form:

http://servername01.home.local/tech-myobjectapi-0.3/myObjectApi/api/v2/Object

I also have to pass the body in this JSON format:

    {  
            "resourceName":"treatment_response",

            "parameters":
              [
                {
                  "name":"trial_name",
                  "value":"TRIAL NAME 01"
                },
                {
                  "name":"Identifier",
                  "value":"56-8956"
                }                          
                ]  

          }

The header JSON:

ApplicationName : APP_1

My R script is:

library(RJSONIO)
library(RCurl)
library(httr)
library("jsonlite")

r <- POST("http://servername01.home.local/tech-myobjectapi-0.3/myObjectApi/api/v2/Object", 
          body = '{  
            "resourceName":"treatment_response",

            "parameters":
              [
                {
                  "name":"trial_name",
                  "value":"TRIAL NAME 01"
                },
                {
                  "name":"Identifier",
                  "value":"56-8956"
                }                          
                ]  

          }' ,
          add_headers("ApplicationName : APP_1")
)

stop_for_status(r)
a<-content(r, "text", "application/json", encoding="UTF-8")
cat(a, file = "test.json")
x<-fromJSON(file("test.json", "r"))
mydf<-do.call(rbind, lapply(x$data, data.frame))

Error in curl::curl_fetch_memory(url, handle = handle) : Couldn't resolve host name

j1897
  • 1,507
  • 5
  • 21
  • 41
  • You need to replace the "servername01.home.local" with server credentials or ip of the server where your database server is running. It should be in the form of "http://ip-address:port_number/tech-myobjectapi-0.3/myObjectApi/api/v2/Object" – tushaR Sep 09 '16 at 08:23

0 Answers0