If you need default query parameters you can define them with the following code:
use actix_web::{App, get, HttpResponse, HttpServer, Responder, web, HttpRequest};
...
#[derive(Debug, Deserialize)]
pub struct Params {
orgIds: String
}
...
async fn totd(db_pool: web::Data<Pool>, req: HttpRequest) -> impl Responder {
let params = web::Query::<Params>::from_query(req.query_string())
.unwrap_or(web::Query(Params { orgIds: String::from("2") }));
let org_ids = ¶ms.orgIds;
...
This applies to actix-web = "4"
Alternatively you can also use Query::<HashMap<String, String>>::from_query
to parse the query.
Here is an example using from_query
:
let params = Query::<HashMap<String, String>>::from_query(req.query_string())
.unwrap();
let default_org_id = &String::from("2");
let default_lang = &String::from("en");
let org_ids = params.get("orgIds").unwrap_or(default_org_id);
let lang = params.get("lang").unwrap_or(default_lang);
With this second technique it is easier to deal with default values.