I have the following structures:
/// Structs to hold the configuration for the Mattermost Team API.
#[serde_as]
#[derive(Serialize, Deserialize, Debug)]
pub struct Team {
pub id: String,
pub name: String,
pub display_name: String,
pub description: String,
pub email: String,
#[serde(rename = "type")]
pub raw_type: String,
pub allowed_domains: String,
pub invite_id: String,
pub allow_open_invite: bool,
pub create_at: i64,
pub update_at: i64,
pub delete_at: i64,
pub company_name: String,
pub scheme_id: Option<String>,
#[serde_as(as = "DefaultOnNull")]
pub group_constrained: bool,
pub policy_id: Option<String>,
pub cloud_limits_archived: bool,
}
/// Structs to hold the response of the Mattermost Team API.
#[derive(Serialize, Deserialize, Debug)]
pub struct TeamsResponse {
pub teams: Vec<Team>,
}
I am getting them from a REST API and I use the following function:
pub async fn get_teams(base_url: &str, token: &str) -> TeamsResponse {
let url = format!("{}/api/v4/teams", base_url);
println!("{:?}", url);
let response = Client::new()
.get(&url)
.header(AUTHORIZATION, format!("Bearer {}", token))
.send()
.await;
match response {
Ok(response) => {
let body = response.text().await.unwrap();
println!("{:?}", body);
let teams_response = serde_json::from_str::<TeamsResponse>(&body).unwrap();
teams_response
}
Err(e) => {
eprintln!("Error fetching teams: {}", e);
TeamsResponse { teams: Vec::new() }
}
}
}
When I print the body, I get the following string (with private data replaced):
"[{\"id\":\"XXXX\",\"create_at\":1549396390239,\"update_at\":1715101122327,\"delete_at\":0,\"display_name\":\"YYYY\",\"name\":\"ops\",\"description\":\"\",\"email\":\"\",\"type\":\"O\",\"company_name\":\"\",\"allowed_domains\":\"\",\"invite_id\":\"ZZZZ\",\"allow_open_invite\":true,\"scheme_id\":null,\"group_constrained\":false,\"policy_id\":null,\"cloud_limits_archived\":false}]"
However, when it attempts to deserialize that data, it panic's and returns the following error:
called `Result::unwrap()` on an `Err` value: Error("invalid type: map, expected a sequence", line: 1, column: 1)