Warp | 06 , Query Parameter
Let’s add query parameters to a route path .
Warp | 06 , Query Parameter
Let’s add query parameters to a route path .
(**Youtube Video**)
In cargo.toml :
[dependencies]
tokio = { version = "1", features = ["full"] }
warp = { version = "0.4", features = ["server"] }
serde = { version = "1.0", features = ["derive"] }
We need serde to deserialize json .
In main.rs :
use serde::Deserialize;
use warp::{Filter};
#[tokio::main]
async fn main() {
let app = app();
warp::serve(app)
.run(([0,0,0,0], 3000))
.await;
}
fn app() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
let root = warp::path!().map(|| "Home!");
let hello = warp::get().and(warp::path!("hello" / i32)).and(warp::query::<Person>()).and_then(hello_handler);
root
.or(hello)
}
async fn hello_handler (id: i32, person: Person) -> Result<String , warp::Rejection> {
Ok(format!("Hello {}, your id is {} , your age is {}", person.name, id, person.age))
}
#[derive(Deserialize)]
struct Person {
name: String,
age: i32
}
In the hello route, we use the and method to attach query parameters. Inside that and call, we use the query function together with the turbo-fish syntax to specify the type Person, which is the struct we defined.
To access the query values, the hello_handler function simply takes a parameter named person of type Person. Warp automatically deserializes the query string into this struct.
So when we visit
http://localhost:3000/hello/2?name=jim&age=20,
the response will be:
“Hello jim, your id is 2, your age is 20.”
메타데이터
- post_id
- d235112dfb4e
- slug
- warp-06-query-parameter-d235112dfb4e
- url
- https://medium.com/@mikecode/warp-06-query-parameter-d235112dfb4e
- canonical_url
- https://medium.com/@mikecode/warp-06-query-parameter-d235112dfb4e
- author_url
- https://medium.com/@mikecode
- status
- ok
- fetched_at
- 2026-06-23 03:48:11