Closed tjyang closed 9 months ago
Hello, thanks for report. Replace:
error!("error: {}", e);
with:
println!("error: {}", e);
And send output here.
$ test01
error: network error
thread 'main' panicked at src/main.rs:18:9:
unexpected error
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
$
- I am guessing it was due to check of remote server certificate invalided issue.
curl --request POST \ --insecure \ --url 'https://zabbix03.test.com/zabbix/api_jsonrpc.php' \ --header 'Content-Type: application/json-rpc' \ --data '{"jsonrpc":"2.0","method":"user.login","params":{"username":"Admin","password":"zabbix"},"id":1}' echo
- See R1, Can you add arg/setting like curl's --insecure to skip certificate check ?
R1: https://github.com/seanmonstar/reqwest/issues/182
You can build http client in such way:
let http_client = ClientBuilder::new().danger_accept_invalid_certs(true).build()
Then use in ZabbixApiV6Client::new(...)
.
//use reqwest::blocking::Client; use reqwest::ClientBuilder; use zabbix_api::client::v6::ZabbixApiV6Client; use zabbix_api::client::ZabbixApiClient;
fn main() { // let http_client = Client::new(); let http_client = ClientBuilder::new().danger_accept_invalid_certs(true).build();
let client = ZabbixApiV6Client::new(http_client, "https://1.1.1.1/zabbix/api_jsonrpc.php");
match client.get_auth_session("Admin", "zabbix") { Ok(session) => println!("session: {session}"), Err(e) => { //error!("error: {}", e); println!("error: {}", e); panic!("unexpected error") } } }
- error message
$ cargo build
@lebe-dev , can you pls delete above spam ?
ClientBulder's build method returns Result<Client, Error>, not Client. So, you have to handle this or use .unwrap().
reqwest::blocking::Client
You should use another ClientBuilder -> reqwest::blocking::ClientBuilder
.
My crate doesn't support asynchronous code.
@lebe-dev Thanks for the guidance along the way. it is time for me to use vscode/rust and really understand the rust code in details.
$ test01
session: 28204658b7fb741ff3e91bade6c2fb55
$
use reqwest;
use std::time::Duration;
use zabbix_api::client::v6::ZabbixApiV6Client;
use zabbix_api::client::ZabbixApiClient;
fn main() {
let http_client = reqwest::blocking::Client::builder()
.timeout(Duration::from_secs(10))
.danger_accept_invalid_certs(true)
.build();
let client = ZabbixApiV6Client::new(
http_client.unwrap(),
"https://1.1.1.1/zabbix/api_jsonrpc.php",
);
match client.get_auth_session("Admin", "zabbix") {
Ok(session) => println!("session: {session}"),
Err(e) => {
println!("error: {}", e);
panic!("unexpected error")
}
}
}
@lebe-dev Thanks for creating this framework, I am interested to learn use it.
1 directory, 3 files $ $ cargo install --path . -f
$ which test01 ~/.cargo/bin/test01 $ test01 thread 'main' panicked at src/main.rs:15:9: unexpected error note: run with
RUST_BACKTRACE=1
environment variable to display a backtrace $$ cat Cargo.toml [package] name = "test01" version = "0.1.0" edition = "2021"
See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] log = "0.4.20" reqwest = { version = "0.11.23", features = ["blocking", "json"] } zabbix-api = "0.1.0" $
$ cat src/main.rs use reqwest::blocking::Client; use zabbix_api::client::v6::ZabbixApiV6Client; use zabbix_api::client::ZabbixApiClient; use log::error;
fn main() { let http_client = Client::new();
let client = ZabbixApiV6Client::new(http_client, "https://zbbix03.test.com/zabbix/api_jsonrpc.php");
match client.get_auth_session("Admin", "zabbix") { Ok(session) => println!("session: {session}"), Err(e) => { error!("error: {}", e); panic!("unexpected error") } } $
$ more ~/zbxc-login.sh curl --request POST \ --insecure \ --url 'https://zabbix03.test.com/zabbix/api_jsonrpc.php' \ --header 'Content-Type: application/json-rpc' \ --data '{"jsonrpc":"2.0","method":"user.login","params":{"username":"Admin","password":"zabbix"},"id":1}' echo $
$ ~/zbxc-login.sh {"jsonrpc":"2.0","result":"6b47c2a57d09b187f511d40369xxxxxx","id":1} $