0

I'm fairly new to rust programming, and I'm following the rust book. However, I recently started to make some exercises by myself to "deepen" my understanding.

I came into an issue with Warp, particularly post requests. Basically, what I am trying to do is whenever I make a post requests with two parameters, in this case 2 numbers. I want to return the sum of both as a response.

use std::collections::HashMap;
use std::ptr::hash;
use warp::{Filter};

async fn add_two_numbers(a: u32, b: u32) -> Result<impl warp::Reply, warp::Rejection> {
    Ok(format!("sum {}", a+b))
}

#[tokio::main]
async fn main() {
    let hello = warp::post()
        .and(warp::path("add_numbers"))
        .and(warp::query::<HashMap<u32, u32>>())
        .and(warp::path::end())
        .and_then(add_two_numbers);

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


However, I'm stuck and do not really know how to get the parameters from the query, and pass them into the function or pass the whole HashMap to the function and from there extract the data I need.

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
Dave S.
  • 88
  • 8
  • I'm confused why you went for a HashMap there at all? What kind of query string do you want to accept with that handler? – PitaJ Nov 28 '22 at 21:04

0 Answers0