0

I am trying to write a script in R to download data from a JSON-RPC API.

Unfortunately, this API is for a private company service and I can't disclose its name or URL. The error should be reproducible with almost any JSON-RPC API though.

The API's manual instructs me to test the connection with this request:

{
  "params": {},
  "version": "1.1",
  "method": "getConnectionTest"
}

I'm trying with this code:

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)

Or with this code:

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)

Or with this code:

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)

No matter what I do, I invariably get this response:

$version
[1] "1.1"

$error
$error$name
[1] "JSONRPCError"

$error$message
[1] "No such a method : ''."

$error$code
[1] 302

From the looks of it, but I may be wrong, it looks like the method is formatted wrong and is sent empty. I'm pretty sure the error is mine.

What am I missing?

roccobarbi
  • 300
  • 1
  • 11

1 Answers1

0

A GET request doesn't have a body. If they want you to send a JSON document, you should be using POST

apiResult <-POST(url = apiURL, body = apiConnectionTest, encode = "json")

And FYI, for debugging this stuff, http://requestb.in/ is a useful site that allows you to set up a temp URL where you can GET/POST messages and see what headers and the content of the request that is being seen by the server.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thanks, it was solved with your solution and this version of the apiConnectionTest variable: apiConnectionTest <- toJSON(list(params = "{}", version = "1.1", method = "getConnectionTest")) – roccobarbi Aug 26 '14 at 20:24