use bytes::Bytes;
use http::{Request, Response};
use http_body_util::{Empty, Full};
use http_wire::WireEncode;
fn main() {
println!("=== Synchronous HTTP Encoding Example ===\n");
println!("1. Simple GET request:");
let request = Request::builder()
.method("GET")
.uri("/api/users")
.header("Host", "example.com")
.header("User-Agent", "http_wire/0.2.5")
.body(Empty::<Bytes>::new())
.unwrap();
match request.encode() {
Ok(bytes) => {
let output = String::from_utf8_lossy(&bytes);
println!("{}", output);
println!("Total bytes: {}\n", bytes.len());
}
Err(e) => eprintln!("Error: {}\n", e),
}
println!("2. POST request with JSON body:");
let body = r#"{"name":"John Doe","email":"john@example.com"}"#;
let request = Request::builder()
.method("POST")
.uri("/api/users")
.header("Host", "example.com")
.header("Content-Type", "application/json")
.header("Content-Length", body.len().to_string())
.body(Full::new(Bytes::from(body)))
.unwrap();
match request.encode() {
Ok(bytes) => {
let output = String::from_utf8_lossy(&bytes);
println!("{}", output);
println!("Total bytes: {}\n", bytes.len());
}
Err(e) => eprintln!("Error: {}\n", e),
}
println!("3. GET request with query parameters:");
let request = Request::builder()
.method("GET")
.uri("/api/search?q=rust&limit=10")
.header("Host", "api.example.com")
.body(Empty::<Bytes>::new())
.unwrap();
match request.encode() {
Ok(bytes) => {
let output = String::from_utf8_lossy(&bytes);
println!("{}", output);
println!("Total bytes: {}\n", bytes.len());
}
Err(e) => eprintln!("Error: {}\n", e),
}
println!("4. HTTP Response - 200 OK:");
let response = Response::builder()
.status(200)
.header("Content-Type", "application/json")
.header("Server", "http_wire/0.2.5")
.body(Full::new(Bytes::from(r#"{"status":"success"}"#)))
.unwrap();
match response.encode() {
Ok(bytes) => {
let output = String::from_utf8_lossy(&bytes);
println!("{}", output);
println!("Total bytes: {}\n", bytes.len());
}
Err(e) => eprintln!("Error: {}\n", e),
}
println!("5. HTTP Response - 404 Not Found:");
let response = Response::builder()
.status(404)
.header("Content-Type", "text/plain")
.body(Full::new(Bytes::from("Resource not found")))
.unwrap();
match response.encode() {
Ok(bytes) => {
let output = String::from_utf8_lossy(&bytes);
println!("{}", output);
println!("Total bytes: {}\n", bytes.len());
}
Err(e) => eprintln!("Error: {}\n", e),
}
println!("6. HTTP Response - 204 No Content:");
let response = Response::builder()
.status(204)
.header("Server", "http_wire")
.body(Empty::<Bytes>::new())
.unwrap();
match response.encode() {
Ok(bytes) => {
let output = String::from_utf8_lossy(&bytes);
println!("{}", output);
println!("Total bytes: {}\n", bytes.len());
}
Err(e) => eprintln!("Error: {}\n", e),
}
println!("=== All examples completed successfully ===");
}