Rust
You need to install reqwest crate and enable its blocking feature.
Run in terminal:
cargo add reqwestThen open Cargo.toml and modify this line in [dependencies] section:
reqwest = { version = "<Already installed version>", features = ["blocking", "json"] }Replace <Already installed version> with number you've had after reqwest installation.
Send Message via REST API
rust
use std::collections::HashMap;
use reqwest::blocking::Client;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new();
let mut data = HashMap::new();
data.insert("dock_id", "<Dock ID>");
data.insert("secret_key", "<Secret Key>");
data.insert("msg", "Hello from Rust!");
let res = client.post("https://api.pingdock.io/ping")
.json(&data)
.send()?;
println!("{}\n{}", res.status(), res.text()?);
Ok(())
}Send Message via Webhook
For this example you need to add serde_json crate.
Run in terminal:
cargo add serde_jsonNow you can run an example:
rust
use reqwest::blocking::Client;
use reqwest::header::CONTENT_TYPE;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new();
let payload = serde_json::json!({ "msg": "PingDock webhook from Rust!" });
let res = client.post("https://api.pingdock.io/webhook")
.basic_auth("<Dock ID>", Some("<Secret Key>"))
.header(CONTENT_TYPE, "application/json")
.json(&payload)
.send()?;
println!("{}\n{}", res.status(), res.text()?);
Ok(())
}