Post Request Params Actix Web

I'm trying to read in parameters from a post request, but I'm getting some weird errors with it not getting deserialized correctly. Anything I'm doing wrong from at a glance?

// Card Command Codes
#[derive(Debug, Deserialize)]
enum CardCmd {
    READ = 256,
    REGISTER = 512,
    REISSUE = 1536,
}

#[derive(Debug, Deserialize)]
pub struct CardVals {
    cmd_str: CardCmd, // Commands for cards
    card_no: u64, // Card from scanner
    // Example: 7020392002385103
}

#[post("/service/card/cardn.cgi")]
async fn cardn(params: web::Query<CardVals>) -> HttpResponse {
    println!("CARDN!");
    //let params = web::Query::<CardVals>::from_query(req.query_string()).unwrap();
    println!("{:?}",params);
    // Parse params and respond accordingly
    dbg!(&params.cmd_str);
    match params.cmd_str {
        CardCmd::READ => {
            println!("READ");
            resp!("")
        },
        CardCmd::REGISTER => {
            println!("REGISTER");
            resp!("")
        },
        CardCmd::REISSUE => {
            println!("REISSUE");
            resp!("")
        }
    }
}

This should return something:

curl -d 'cmd_str=1536' -d 'card_no=12345678' -X POST http://10.3.0.53/service/car
d/cardn.cgi
Query deserialize error: missing field `cmd_str`

I realized. This curl command used -d which is post data. I'm not reading in post data. I'm reading in params! OOPS!

1 Like

Query is not the right extractor to use to extract the body of a POST request. Try using Form instead (when you are using a x-www-form-urlencoded body).

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.