I'm trying to learn how to make calls to an API and there is one thing that I can figure out for all the tutorials.
Cargo.toml
[package]
name = "prj_the_muse_api"
version = "0.1.0"
edition = "2021"
[dependencies]
postgres = "0.19.5"
reqwest = { version = "0.11", features = ["json"] } # reqwest with JSON parsing support
futures = "0.3" # for our async / await blocks
tokio = { version = "1.12.0", features = ["full"] } # for our async runtime
serde_json = "1.0.95"
serde = { version = "1.0.159", features = ["derive"] }
mod_api_request_calls.rs
use std::error::Error;
use std::num::ParseIntError;
use reqwest::header::CONTENT_TYPE;
use reqwest::header::ACCEPT;
use reqwest::header::AUTHORIZATION;
use serde::{Deserialize, Serialize};
use std::str;
use std::mem;
#[tokio::main]
pub async fn do_api_stuff4() -> Result<(), Box<dyn Error>> {
let client = reqwest::Client::new();
let response = client
.get("https://www.themuse.com/api/public/jobs?page=1")
.header(AUTHORIZATION, "Bearer [AUTH_TOKEN]")
.header(CONTENT_TYPE, "application/json")
.header(ACCEPT, "application/json")
.send()
.await
.unwrap()
.text()
.await?;
match response.status() {
reqwest::StatusCode::OK => {
println!("Success! {:?}");
},
reqwest::StatusCode::UNAUTHORIZED => {
println!("Need to grab a new token");
},
_ => {
panic!("Uh oh! Something unexpected happened.");
},
};
Ok(())
}
I understand that when I am using the response
object (of datatype reqwest::Client
) I need to check if anything has gone wrong and so I use the match response.status()
and check against different status codes.
However it seems like the response
object doesn't have a method called status()
.
error[E0599]: no method named `status` found for struct `String` in the current scope
--> src/mod_api_request_calls.rs:196:20
|
196 | match response.status() {
| ^^^^^^ method not found in `String`
For more information about this error, try `rustc --explain E0599`.
Why doesn't it have a method called status? Do I need to add a use statement or need to add something into Cargo.toml (with a Cargo add ...
command)?
I've seen a few tutorials where they use a status
method, but this object doesn't seem to have a status
method?
I'm confused, what am I missing?