Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Reqwest json handle empty response
So I have a page with JSON data for multiple devices, I retrieve the JSON and it gets "mapped" to the corresponding Struct, worked good but sometimes there will be no JSON available for a given device (this is to be expected) and since I'm using unwrap()
rust will panic when this happens.
called Option::unwrap() on a None value
What would be the correct way to approach this? I'm thinking of using match
but I'm unsure.
pub async fn get_alerts_device(device: u8) -> Result<Vec<MyAlerts>, reqwest::Error> {
let client = reqwest::Client::new();
let res = client
.get("https://example.com/json-page")
.send()
.await?;
let end: Device = res.json().await.unwrap();
let deviceMap = end
.device
.get_key_value(format!("device-{}", device).as_str())
.unwrap()
.1;
Ok(antennaMap.clone())
}
I've included the code in the function I'm mentioning so i hope it doesn't clutter to much. But ofcourse the focus lies with res.json().await.unwrap();
Thank you for your time and patience, I'm re-reading the book chapter on error handling as we speak!
6 posts - 3 participants
🏷️ Rust_feed