2013-04-18 3 views
0

Zabbix 서비스에서 일부 데이터를 소비하려고합니다. NodeJS와 함께 작동하지만 Dart와는 아무것도받지 못합니다. 어쩌면 내가 뭔가를 놓치고 있지만, 내 애플 리케이션 의이 부분과 많은 시간을 보냈습니다. 나는 M4 Dart 편집기를 사용하고있다. 이것은 내 다트 코드 :다트 HttpClient가 게시되지 않습니다.

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
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)); 
}); 
관련 문제