当我运行以下代码时:
use exitfailure::ExitFailure;
use reqwest::Url;
use serde_derive::{Deserialize, Serialize};
use std::env;
#[derive(Serialize, Deserialize, Debug)]
struct CompanyInfo {
country: String,
currency: String,
exchange: String,
ipo: String,
marketCapitalization: u128,
name: String,
phone: String,
shareOutstanding: f64,
ticker: String,
weburl: String,
logo: String,
finnhubIndustry: String,
}
impl CompanyInfo {
async fn get(symbol: &String, api_key: &String) -> Result<Self, ExitFailure> {
let url = format!(
"https://finnhub.io/api/v1/stock/profile2?symbol={}&token={}",
symbol, api_key
);
let url = Url::parse(&*url)?;
let res = reqwest::get(url).await?.json::<CompanyInfo>().await?;
Ok(res)
}
}
#[tokio::main]
async fn main() -> Result<(), ExitFailure> {
let api_key = "MY API KEY".to_string();
let args: Vec<String> = env::args().collect();
let mut symbol: String = "AAPL".to_string();
if args.len() < 2 {
println!("Since you didn't specify a company symbol, it has defaulted to AAPL.");
} else {
symbol = args[1].clone();
}
let res = CompanyInfo::get(&symbol, &api_key).await;
println!("{:?}", res);
Ok(())
}
我收到一个错误:Err(错误解码响应正文:第 1 行第 235 列的预期“,”或“}”)。对于另一个 API,这个具有类似结构的代码可以工作。你如何用 reqwest 解决这个问题?