lox_cli/src/hyper_client_net.rs

26 lines
888 B
Rust

// This file provides networking using hyper (which
// https://gitlab.torproject.org/onyinyang/lox-server uses).
// During a later cleanup, this will replace client_net.rs.
use hyper::{Body, Client, Method, Request};
pub async fn net_request(url: String, body: Vec<u8>) -> Vec<u8> {
let client = Client::new();
let uri = url.parse().expect("Failed to parse URL");
let resp = if body.len() > 0 {
// make a POST with a body
let req = Request::builder().method(Method::POST).uri(uri).body(Body::from(body)).expect("Failed to create POST request");
client.request(req).await.expect("Failed to POST")
} else {
// make a GET request
client.get(uri).await.expect("Failed to GET")
};
println!("Response: {}", resp.status());
let buf = hyper::body::to_bytes(resp).await.expect("Failed to concat bytes");
buf.to_vec()
}