2016-04-15 9 views
1

Content-Typeは(これはjsonとは異なります)Content-Typeに設定する必要がありますと私はZabbix APIと考えています。https POSTでbodyを指定する方法json-rpcアプリケーションのコンテンツタイプ[R]

私が正しく動作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}' 

体(-dhttrパッケージを使用して、今

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

と私はRでこれを実行しようとしていますと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にこのボディパラメータを正しく指定する方法を知っていますか?

+0

[curlconverter](https://cran.r-project.org/web/packages/curlconverter/index.html)に試してみてください。これは、カールコマンドライン操作をR関数に変換します。 – Thomas

+0

私はそれをチェックして、エラー: 'Unixのようなシステム上のクリップボードは 'xclip'(推奨)または 'xsel'を必要としているので、私の問題には関係しないと思う: –

+0

答えはここに掲載されているhttp:// stackoverflow.com/a/36697667/3857701 –

答えて

0

いいえ、encode = "json"パラメータ(コメントアウトされています)は指定されていませんでした。各ベクトルにjsonlite::unboxと入力する必要はありませんが、data.frameの場合はparamsのように入力してください。

関連する問題