Update rest-get.md

Fixed runtime decoding error occurring due to 403 responses from gh api because of missing user-agent header
This commit is contained in:
suren-m 2022-04-01 19:37:52 +01:00 committed by GitHub
parent 752b035c1a
commit 5b9b625ee3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -12,6 +12,7 @@ processing the response into User instances.
```rust,edition2018,no_run ```rust,edition2018,no_run
use serde::Deserialize; use serde::Deserialize;
use reqwest::Error; use reqwest::Error;
use reqwest::header::USER_AGENT;
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
struct User { struct User {
@ -25,7 +26,13 @@ async fn main() -> Result<(), Error> {
owner = "rust-lang-nursery", owner = "rust-lang-nursery",
repo = "rust-cookbook"); repo = "rust-cookbook");
println!("{}", request_url); println!("{}", request_url);
let response = reqwest::get(&request_url).await?;
let client = reqwest::Client::new();
let response = client
.get(request_url)
.header(USER_AGENT, "rust-web-api-client") // gh api requires a user-agent header
.send()
.await?;
let users: Vec<User> = response.json().await?; let users: Vec<User> = response.json().await?;
println!("{:?}", users); println!("{:?}", users);