Get array of HTML checkboxes in Actix-Web?

Given the following HTML

<div><input type="checkbox" value="sushi" name="restaurant_categories[]"> Sushi</div>
<div><input type="checkbox" value="pizza" name="restaurant_categories[]"> Pizza</div>
<div><input type="checkbox" value="italian" name="restaurant_categories[]"> Italiaans</div>
<div><input type="checkbox" value="kebab" name="restaurant_categories[]"> Kebab</div>

I am trying to retrieve the selected checkbox-values. In PHP I'd just do $_POST["restaurant_categories[]"].

I tried the following in Actix/web:

#[derive(Deserialize)]
pub struct AddRestaurantForm {
    //...
    pub restaurant_categories: std::vec::Vec<String>,
}

But that gives the runtime error: Parse error: missing field `restaurant_categories`. .

How would I solve this?

How are you sending the form?

As far as I can tell, actix-web's Form extractor doesn't support this. In your example, it will try to find a restaurant_categories field with a list of strings inside, but instead the request contains multiple fields named restaurant_categories[]. You could try something like serde_qs - Rust

1 Like

I solved it by sending it through AJAX as JSON. Easier.

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.