如何使用REST API从R中的数据库中发布和检索数据?

时间:2016-09-09 05:47:13

标签: json r api jsonlite rjson

我需要在R中调用一个API,它会向数据库服务器发出请求并将结果提取到R数据帧中。

我拥有的API格式为:

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

我还必须以这种JSON格式传递正文:

    {  
            "resourceName":"treatment_response",

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

          }

标题JSON:

ApplicationName : APP_1

我的R脚本是:

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))
  

curl :: curl_fetch_memory(url,handle = handle)出错:无法解决   解析主机名

0 个答案:

没有答案