最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rust - expected a sequence or missing field for query string parameter in Actix Web - Stack Overflow

programmeradmin1浏览0评论

I am trying to implement an Actix Web endpoint that accepts a vector of parameters from the query string.

use actix_web::{get, web, App, HttpResponse, HttpServer};
use serde::Deserialize;

#[derive(Deserialize)]
struct Query {
    x: Vec<i32>,
}

#[get("/test")]
async fn test(query: web::Query<Query>) -> HttpResponse {
    println!("{:?}", query.x);
    HttpResponse::Ok().finish()
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new().service(test)
    }).bind(("localhost", 8080))?.run().await
}

If I call it by http://localhost:8080/test?x=25 I obviously get the logical error:

Query deserialize error: invalid type: string "25", expected a sequence

But in case of http://localhost:8080/test?x[]=25&x[]=36 the error is:

Query deserialize error: missing field `x`

It looks confusing. How am I supposed to pass the multiple x in the handler?

My Cargo.toml is:

[package]
name = "actix-qs-check"
version = "0.1.0"
edition = "2024"

[dependencies]
actix-web = "4.9.0"
serde = { version = "1.0.217", features = ["derive"] }

The toolchain:

rustc 1.85.0-nightly (21fe748be 2024-12-11)

I also found a solution to use serde_qs (here: ). It actually helps, but since it was too much time before I hope, there's a better solution. Maybe some versions mismatch or I fot some important options...

发布评论

评论列表(0)

  1. 暂无评论