reqwest::Client where is the status method?

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?

In the client.get()... call chain, you extract the contents of the response struct with .text(), so it's a String from then on, not a Response.

1 Like

Brilliant, thank you for that.

Note that, most of the time, you'll want to check for bad HTTP statuses via the Response::error_for_status() method, so your code would become:

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()
        .error_for_status()?  // New line
        .text()
        .await?;

OK, cool thanks for that.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.