如何在httr POST中为json-rpc应用程序内容类型指定body [R]

时间:2016-04-15 16:03:03

标签: json r curl httr

我正在使用Zabbix API我认为Content-Type应该设置为application/json-rpc(这与json不同)。

我创建了一个正常运行的curl请求

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)是在R中用

创建的
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)))

现在我尝试使用httr包在R中运行此功能,但这似乎json类型的传统正文规范不适用于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}

有人知道如何在httr::POST中为此卷曲请求正确指定此body参数吗?

1 个答案:

答案 0 :(得分:0)

好的,这是一个未指定encode = "json"参数的问题(已被注释掉)。一个人不必为每个向量键入jsonlite::unbox,但这对data.frame来说是强制性的,例如params