Skip to navigation
Simple Rust CGI example what can receive a post request
22.08.23
You need to have the CGI file behind a web server, so it can execute and serve it and environment you can get from http://calantas.org/rust_cgi_example ## Cargo.toml ``` [package] name = "cgi" version = "0.1.0" authors = ["veto"] edition = "2021" [dependencies] cgi = "0.6" serde = { version = "*", features = ["derive"] } serde_json = "*" serde_urlencoded = "0.5.1" ``` ## src/main.rs ```rust extern crate cgi; use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug, Default)] struct Email { id: String, name: String, } fn main() { // curl --data "id=value1" --data "name=value2" http://127.0.0.1/email.cgi cgi::handle(|req: cgi::Request| -> cgi::Response { println!("xxxxxxxxxxxxxxxxxxxxxxxxxxx"); let b = req.body(); // General Serialize Key Value //let obj = serde_urlencoded::from_bytes::
>(b); // Serialize From Custom Struct Eamil let obj = serde_urlencoded::from_bytes::
(b); let o = &obj.unwrap(); let id = &o.id; let name = &o.name; println!("{:?}", id); println!("{:?}", name); println!("xxxxxxxxxxxxxxxxxxxxxxxxxxx"); cgi::html_response(200, format!("
Test
")) }) } ``` ## TEST ```console curl --data "id=value1" --data "name=value2" http://127.0.0.1/email.cgi ```
https://rust.velas.com/serde_urlencoded/fn.from_bytes.html
Reply
Anonymous
Information Epoch 1732410543
Design for visibility.
Home
Notebook
Contact us