0

我只想从以下 URL 获取 JSON。

所以我使用了这段代码:

extern crate reqwest;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let res = reqwest::Client::new()
        .get("https://api.github.com/users/octocat")
        .send()?
        .text()?;
    println!("{}", res);

    Ok(())
}

但我不知道如何解决错误:

error[E0277]: the `?` operator can only be applied to values that implement `std::ops::Try`
  --> src\main.rs:19:15
   |
19 |       let res = reqwest::Client::new()
   |  _______________^
20 | |         .get("https://api.github.com/users/octocat")
21 | |         .send()?
   | |________________^ the `?` operator cannot be applied to type `impl std::future::Future`
   |
   = help: the trait `std::ops::Try` is not implemented for `impl std::future::Future`
   = note: required by `std::ops::Try::into_result`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: could not compile `Desktop`.

但我也可以通过简单的方式获得我想要的

curl https://api.github.com/users/octocat

我尝试添加use std::ops::Try;,但效果不佳。

4

1 回答 1

3

reqwestcrate 默认提供异步 api 。因此,您必须.await在与操作员一起处理错误之前?。您还必须使用异步运行时,例如tokio

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
  let resp = reqwest::Client::new()
    .get("https://api.github.com/users/octocat")
    .send()
    .await?
    .json::<std::collections::HashMap<String, String>>()
    .await?;
  println!("{:#?}", resp);
  Ok(())
}

请注意,如上所示,要使用将响应转换为 json,您必须json在您的 : 中启用该功能Cargo.toml

reqwest = { version = "0.10.8", features = ["json"] }

如果您不想使用异步运行时,可以启用阻塞reqwest客户端:

[dependencies]
reqwest = { version = "0.10", features = ["blocking", "json"] }

并像这样使用它:

fn main() -> Result<(), Box<dyn std::error::Error>> {
  let resp = reqwest::blocking::Client::new()
    .get("https://api.github.com/users/octocat")
    .send()?
    .json::<std::collections::HashMap<String, String>>()?;
  println!("{:#?}", resp);
  Ok(())
}

Github 的 api 需要几个其他的配置选项。这是 reqwest 和 github api 的最小工作示例:

use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT};
use serde::{Deserialize};

fn main() -> Result<(), Box<dyn std::error::Error>> {
  let mut headers = HeaderMap::new();
  // add the user-agent header required by github
  headers.insert(USER_AGENT, HeaderValue::from_static("reqwest"));

  let resp = reqwest::blocking::Client::new()
    .get("https://api.github.com/users/octocat")
    .headers(headers)
    .send()?
    .json::<GithubUser>()?;
  println!("{:#?}", resp);
  Ok(())
}

// Note that there are many other fields
// that are not included for this example
#[derive(Deserialize, Debug)]
pub struct GithubUser {
  login: String,
  id: usize,
  url: String,
  #[serde(rename = "type")]
  ty: String,
  name: String,
  followers: usize
}
于 2020-11-03T16:30:35.450 回答