Rust beginner here. I'm trying to make a post request using basic auth with reqwest.
This is my code
use std::error::Error;
use reqwest::Client;
use reqwest::Error as rError;
use futures::executor::block_on;
const URL_BASE: &str = "REDACTED";
const USERNAME: &str = "REDACTED";
const PASSWORD: &str = "REDACTED";
pub fn get_token() {
let res = block_on(get_token_async());
match res {
Ok(_) => println!("Success"),
Err(e) => {
eprintln!("{}", e);
if let Some(source) = e.source() {
eprintln!("{}", source);
}
},
}
}
pub async fn get_token_async() -> Result<(), rError> {
let client = Client::new();
let url = String::from(URL_BASE.to_owned() + "/uaa/oauth/token");
let response = client
.get(url)
.basic_auth(USERNAME, Some(PASSWORD))
.query(&[("grant_type", "client_credentials")])
.send()
.await?;
println!("{}", response.status());
let token = response.text().await?;
println!("{}", token);
Ok(())
}
Main function
mod modulename;
#[tokio::main]
async fn main() {
modulename::get_token();
}
Cargo.toml
[package]
name = "hist_migr"
version = "0.1.0"
edition = "2021"
[dependencies]
futures = "0.3.30"
reqwest = {version = "0.12.5", features = ["json"]}
tokio = {version = "1.39.2", features = ["full"]}
When I run it I get:
error sending request for url (REDACTED/uaa/oauth/token?grant_type=client_credentials)
client error (Connect)
The same link copy and pasted from the error into a browser works fine, returning the expected data...
Is there any way to get a more detailed description than just "client error"? If I can't know why the error happened or get the status code, I have no way of troubleshooting (especially since it works fine elsewhere). I'd appreciate any advice or direction, because at this point I'm out of ideas... Thank you in advance.
Docs say that default-tls is enabled by default, but I added features = ["json", "default-tls", "native-tls", "rustls-tls"]} anyways. Still the same error. Is that error really as descriptive as its going to be?
Placeholders for string formatting are documented in std::fmt. The ? format prints whatever the value's implementation of Debug prints, and most errors have a Debug implementation that gives extensive detail of the error, whereas the Display implementation (used by the {} placeholder) may only give a high-level error message.