我正在尝试在R中编写脚本以从JSON-RPC API下载数据。
很遗憾,此API适用于私人公司服务,但我无法透露其名称或网址。几乎所有JSON-RPC API都应该可以重现该错误。
API手册指示我测试与此请求的连接:
{
"params": {},
"version": "1.1",
"method": "getConnectionTest"
}
我正在尝试使用此代码:
library(httr)
apiURL <- "http://private.url/found/in/the/manual/JSONRPC.cgi"
apiConnectionTest <- list(params = "", version = "1.1", method = "getConnectionTest")
apiGETConnectionTest <- GET(url = apiURL, body = apiConnectionTest, encode = "json")
content(apiGETConnectionTest)
或使用此代码:
library(httr)
apiURL <- "http://private.url/found/in/the/manual/JSONRPC.cgi"
apiConnectionTest <- list(params = "{}", version = "1.1", method = "getConnectionTest")
apiGETConnectionTest <- GET(url = apiURL, body = apiConnectionTest, encode = "json")
content(apiGETConnectionTest)
或使用此代码:
library(httr)
library(rjson)
apiURL <- "http://private.url/found/in/the/manual/JSONRPC.cgi"
apiConnectionTest <- toJSON(list("params" = "{}", "version" = "1.1", "method" = "getConnectionTest"))
apiGETConnectionTest <- GET(url = apiURL, body = apiConnectionTest, encode = "json")
content(apiGETConnectionTest)
无论我做什么,我都会得到这样的回应:
$version
[1] "1.1"
$error
$error$name
[1] "JSONRPCError"
$error$message
[1] "No such a method : ''."
$error$code
[1] 302
从它的外观来看,但我可能是错的,看起来该方法格式错误并被发送为空。我很确定错误是我的。
我错过了什么?
答案 0 :(得分:0)
GET
请求没有正文。如果他们希望您发送JSON文档,则应使用POST
apiResult <-POST(url = apiURL, body = apiConnectionTest, encode = "json")
和FYI,为了调试这些东西,http://requestb.in/是一个有用的站点,允许您设置临时URL,您可以在其中获取/发布消息,并查看正在查看的请求的标题和内容由服务器。