Search code examples
jsonrcurlhttr

How to specify body in httr POST for json-rpc application content type [R]


I am working with Zabbix API where I think that Content-Type should be set to application/json-rpc (whatever this differs from json).

I have created a curl request that works properly

curl -H "Content-Type:application/json-rpc"             \
     -X POST https://zabbix_host/api_jsonrpc.php \
     -v                                             \
     -A "UserAgentJakis"  \
     -d '{"jsonrpc":"2.0","method":"history.get","params":{"output":"extend","itemids":"100100001196479","history":0,"sortfield":"clock","sortorder":"DESC","limit":10},"auth":"3421b7f82e38323506264018de256bdd","id":1}'

body (-d) was created in R with

toJSON(list(jsonrpc = jsonlite::unbox("2.0"),
                        method = jsonlite::unbox("history.get"),
                        params = jsonlite::unbox(
                            data.frame(output = "extend",
                                                itemids = "100100001196479",
                                                history = 0,
                                                sortfield = "clock",
                                                sortorder = "DESC",
                                                limit = 10)),
                        auth = jsonlite::unbox("3421b7f82e38323506264018de256bdd"),
                        id = jsonlite::unbox(1)))

and now I am trying to run this in R using httr package but this looks like traditional body specification for json type does not work for json-rpc

> httr::POST(url,
+        content_type('application/json-rpc'),
+        #encode = "json",
+        user_agent("example"),
+        body = 
+        list(jsonrpc = jsonlite::unbox("2.0"),
+               method = jsonlite::unbox("history.get"),
+               params = jsonlite::unbox(
+                   data.frame(output = "extend",
+                                       itemids = "100100001196479",
+                                       history = 0,
+                                       sortfield = "clock",
+                                       sortorder = "DESC",
+                                       limit = 10)),
+                 auth = jsonlite::unbox("3421b7f82e38323506264018de256bdd"),
+                 id = jsonlite::unbox(1)),
+        verbose()
+ ) -> zabbix_response
-> POST /api_jsonrpc.php HTTP/1.1
-> User-Agent: example
-> Host: /i_have_deleted_that/
-> Accept-Encoding: gzip, deflate
-> Accept: application/json, text/xml, application/xml, */*
-> Content-Length: 564
-> Expect: 100-continue
-> Content-Type: application/json-rpc; boundary=------------------------468f366928125e3c
-> 
<- HTTP/1.1 100 Continue
>> --------------------------468f366928125e3c
>> Content-Disposition: form-data; name="jsonrpc"
>> 
>> 2.0
>> --------------------------468f366928125e3c
>> Content-Disposition: form-data; name="method"
>> 
>> history.get
>> --------------------------468f366928125e3c
>> Content-Disposition: form-data; name="params"
>> 
>> 1
>> --------------------------468f366928125e3c
>> Content-Disposition: form-data; name="auth"
>> 
>> 3421b7f82e38323506264018de256bdd
>> --------------------------468f366928125e3c
>> Content-Disposition: form-data; name="id"
>> 
>> 1
>> --------------------------468f366928125e3c--

<- HTTP/1.1 200 OK
<- Server: nginx/1.8.0
<- Date: Fri, 15 Apr 2016 16:00:30 GMT
<- Content-Type: application/json
<- Transfer-Encoding: chunked
<- Connection: close
<- X-Powered-By: PHP/5.5.23
<- Content-Encoding: gzip
<- 
> zabbix_response$request$output
list()
attr(,"class")
[1] "write_memory"   "write_function"
> cat(rawToChar(zabbix_response$content))
{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error","data":"Invalid JSON. An error occurred on the server while parsing the JSON text."},"id":null}

Does anybody know how to properly specify this body parameter in httr::POST for this curl request?


Solution

  • Ok it was an issue with not specified encode = "json" parameter (which was commented out). One does not have to type jsonlite::unbox for each vector but this is obligatory for data.frames like params here.