Hyper uri encoded

I have

let url = "".parse::<hyper::Uri>()?;

I get a response from the server 404 Not Found

I see a request in the server logs "GET http%3A// HTTP/1.1" 404 Not Found

I'm just learning rust. I'm studying tokio and hyper. What am I doing wrong?

That's creating an Uri but how are you using it to make your request?

I am studying an example from the hyper page

    // Parse our URL...
    let url = "".parse::<hyper::Uri>()?;

    // Get the host and the port
    let host = url.host().expect("uri has no host");
    let port = url.port_u16().unwrap_or(80);
    let path = url.path();
    let scheme = url.scheme_str().expect("uri has no scheme");
    println!("scheme: {}", &scheme);
    println!("host: {}", &host);
    println!("port: {}", &port);
    println!("path: {}", &path);

    let address = format!("{}:{}", host, port);

    // Open a TCP connection to the remote host
    let stream = TcpStream::connect(address).await?;

    // Use an adapter to access something implementing `tokio::io` traits as if they implement
    // `hyper::rt` IO traits.
    let io = TokioIo::new(stream);

    // Perform a TCP handshake
    let (mut sender, conn) = hyper::client::conn::http1::handshake(io).await?;

    // Spawn a task to poll the connection, driving the HTTP state
    tokio::task::spawn(async move {
        if let Err(err) = conn.await {
            println!("Connection failed: {:?}", err);

    // The authority of our URL will be the hostname of the httpbin remote
    let authority = url.authority().unwrap().clone();

    // Create an HTTP request with an empty body and a HOST header
    let req = Request::builder()
        .header(hyper::header::HOST, authority.as_str())

    // Await the response...
    let mut res = sender.send_request(req).await?;

    println!("Response status: {}", res.status());

    // Stream the body, writing each frame to stdout as it arrives
    while let Some(next) = res.frame().await {
        let frame = next?;
        if let Some(chunk) = frame.data_ref() {

I discovered the following moment:

From the application on rust in the server logs, an entry of the following type - "POST /api/v1/report/base_metrics HTTP/1.1" 200 OK

From the application on python in the server logs, an entry of the following type - "GET http%3A// HTTP/1.1" 404 Not Found

scheme, host and port come as part of the path.

I don't know if I found the right solution, but if I pass the path to the uri instead of the url, then everything works correctly.

let req = Request::builder()
        .header(hyper::header::HOST, authority.as_str())
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.