0

我正在尝试使用 Zabbix 服务中的一些数据。它适用于 NodeJS,但使用 Dart 我什么也没收到。也许我遗漏了一些东西,但我在我的应用程序的这一部分上花了很多时间。我正在使用 M4 Dart 编辑器(顺便说一句很棒)。这是我的飞镖代码:

const String path = "http://someIp/zabbix/api_jsonrpc.php";

Map zabbix = {
      "jsonrpc": "2.0",
      "method": "user.authenticate",
      "params": {
        "user": "user",
        "password": "password"
      },
      "id": 1,
      "auth": null
    };

HttpClient cliente = new HttpClient();

    cliente.postUrl( new Uri.fromString( path ))
        .then(( HttpClientRequest req ) {
          req.headers.contentType = new ContentType( "application", "json-rpc", charset: "utf-8" );
          req.headers.add( HttpHeaders.CONNECTION, "keep-alive");
          req.write( json.stringify( zabbix ));
          return req.close();
      }).then(( HttpClientResponse res ) {
        StreamSubscription st = res.listen( null );

        st.onData(( chunk ) => print( chunk));
      });

这是我的 Nodejs 代码:

var opt = {
        host: 'someIp',
        path: '/zabbix/api_jsonrpc.php',
        method: 'POST',
        headers: {
            'content-type': 'application/json-rpc'
        }
    };

var zabbix = {
            "jsonrpc": "2.0",
            "method": "user.authenticate",
            "params": {
                "user": "user",
                "password": "password"
            },
            "id": 1,
            "auth": null
        },
        req = http.request(opt, function( res ) {
          res.setEncoding('utf8');

          res.on('data', function(chunk) {
            console.log(chunk);
          });
        });

    req.write(JSON.stringify(zabbix), 'utf8');
    req.end();
4

1 回答 1

1
import 'dart:convert';

cliente.postUrl( Uri.parse( path ))
  .then(( HttpClientRequest req ) {
    req.headers.contentType = new ContentType( "application", "json-rpc", charset: "utf-8" );
    req.headers.add( HttpHeaders.CONNECTION, "keep-alive");
    req.write( JSON.encode( zabbix ));
    return req.close();
  }).then(( HttpClientResponse res ) {
    StreamSubscription st = res.listen( null );

    st.onData(( chunk ) => print( chunk));
});
于 2014-02-26T05:59:42.983 回答