-4

Both of the following "hello world" samples build successfully, but only the first results in a page that says "Hello World"; the second one gives me an error about the page not being found. The Cargo.toml file is the same for both. I'm accessing the IP 127.0.0.1:3030.

I tried accessing the second one using curl, but it returned nothing and went back to the prompt. The browser I used for the successful first one is Microsoft Edge 84.

Why doesn't the second one work?

First one

#![deny(warnings)]
use warp::Filter;

#[tokio::main]
async fn main() {
    // Match any request and return hello world!
    let routes = warp::any().map(|| "Hello, World!");

    warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;

Second one

use warp::Filter;

#[tokio::main]
async fn main() {
    // GET /hello/warp => 200 OK with body "Hello, warp!"
    let hello = warp::path!("hello" / String)
        .map(|name| format!("Hello, {}!", name));

    warp::serve(hello)
        .run(([127, 0, 0, 1], 3030))
        .await;
}

Cargo.toml

[package]
name = "warptest"
version = "0.1.0"
authors = ["user"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html


[dependencies]

tokio = { version = "0.2", features = ["full"] }
warp = "0.2"
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
warsong
  • 1,008
  • 2
  • 12
  • 21
  • 2
    *I'm accessing the IP 127.0.0.1:3030* — you haven't stated what **path** you are accessing, so I have to assume you mean `http://127.0.0.1:3030/`. Why do you believe that the second example will respond to this path when you've only configured it for `/hello/:some-string`? – Shepmaster Aug 24 '20 at 19:39
  • I thought it was pretty obvious I was trying to access the same path or IP across both code samples. I didn't configure the 2nd one, I took them from different parts of the official warp docs https://docs.rs/warp/0.2.4/warp/ – warsong Aug 24 '20 at 19:42
  • *I was trying to access the same path or IP across both code samples* — yes, but it's not clear what **path** you are trying to access. For example, the **path** of this post is `/questions/63567567` and the **path** of the homepage is `/`. The **host** is `stackoverflow.com`. You need many pieces to construct a full URL. – Shepmaster Aug 24 '20 at 19:51
  • "but it's not clear what path you are trying to access" from what I could tell from the code in the example, the correct path was the 127.0.0.1:3030 one. I didn't realize I should add in the resource or whatever. – warsong Aug 24 '20 at 19:53

1 Answers1

2

The second example only responds to requests at the path /hello/string-argument. It is not configured to respond to the root path /:

% curl -vvvv 'http://0.0.0.0:3030/'
*   Trying 0.0.0.0...
* TCP_NODELAY set
* Connected to 0.0.0.0 (127.0.0.1) port 3030 (#0)
> GET / HTTP/1.1
> Host: 0.0.0.0:3030
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 404 Not Found
< content-length: 0
< date: Mon, 24 Aug 2020 19:53:06 GMT
<
* Connection #0 to host 0.0.0.0 left intact
* Closing connection 0
% curl -vvvv 'http://0.0.0.0:3030/hello/world'
*   Trying 0.0.0.0...
* TCP_NODELAY set
* Connected to 0.0.0.0 (127.0.0.1) port 3030 (#0)
> GET /hello/world HTTP/1.1
> Host: 0.0.0.0:3030
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
< content-type: text/plain; charset=utf-8
< content-length: 13
< date: Mon, 24 Aug 2020 19:54:01 GMT
<
* Connection #0 to host 0.0.0.0 left intact
Hello, world!* Closing connection 0
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366