Dart HttpClient不是POSTing

时间:2013-04-18 15:42:13

标签: httpclient dart zabbix

我正在尝试从Zabbix服务中获取一些数据。它与NodeJS一起工作但是Dart我什么都没收到。也许我错过了什么,但我花了很多时间在我的应用程序的这一部分。我正在使用M4 Dart编辑器(顺便说一句,这很棒)。这是我的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();

1 个答案:

答案 0 :(得分: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));
});