Warp: Query string example

Created on 23 Jan 2019  路  9Comments  路  Source: seanmonstar/warp

Could anyone please give me a simple query string deserialization example? I'm was messing around for two hour but still can't find the way.

Most helpful comment

use serde_derive::Deserialize;
use warp::Filter;

fn main() {
    // GET /area?width=25&height=5 => "Area = 125"
    let area = warp::get2().and(warp::path("area"))
        .and(warp::query().map(|r: Rectangle| format!("Area = {}", r.width * r.height)));

    warp::serve(area).run(([0, 0, 0, 0], 3030));
}


#[derive(Deserialize)]
struct Rectangle {
    width: u32,
    height: u32,
}

All 9 comments

Nevermind. I found it.

@nhphu maybe post the example code for people having the same issue 馃槈?

use serde_derive::Deserialize;
use warp::Filter;

fn main() {
    // GET /area?width=25&height=5 => "Area = 125"
    let area = warp::get2().and(warp::path("area"))
        .and(warp::query().map(|r: Rectangle| format!("Area = {}", r.width * r.height)));

    warp::serve(area).run(([0, 0, 0, 0], 3030));
}


#[derive(Deserialize)]
struct Rectangle {
    width: u32,
    height: u32,
}

I was searching for this example for an hour now. I'm glad I found it. Thanks!
Is it possible to add this to the examples directory and/or the documentation on queries?

Can someone provide an example of a filter which combines a parameter _and_ a query string?

And here I am answering my own question as well:

warp::path("rectangle")
    .and(warp::path::param())
    .and(warp::path::end())
    .and(warp::query::<Rectangle>())
    .map(move |measure: String, r: Rectangle| {
        format!("Measure = {}, Width = {}, Height = {}", measure, r.width, r.height)
    });

I also searched for an hour before arriving here. Adding an example would be good I think.

if anyone wants to submit a PR with it :+1:

I submitted a PR.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dylanede picture dylanede  路  3Comments

kamalmarhubi picture kamalmarhubi  路  5Comments

seanmonstar picture seanmonstar  路  8Comments

Apromixately picture Apromixately  路  3Comments

kitsuneninetails picture kitsuneninetails  路  4Comments