How to Add Basic Authorization Header by Passing the Secret Using Reqwest? [Closed]

How to Add Basic Authorization Header by Passing the Secret Using Reqwest? [Closed]

I'm using reqwest = { version = "0.11", features = ["json"] }

impl Client {
    pub fn new(/*endpoint: Url*/) -> Result<Client> {
        Ok(Client {
            client: reqwest::ClientBuilder::new().build()?,
        })
    }
}
let res = self
    .client
    .post(url)
    .header("Content-type", "application/x-www-form-urlencoded")
    .header("Authorization", "Basic".to_owned() + &secret)
    .send
    .await?;
let data = res.json::<Response>().await?;

I'm not able to set the basic authorization header, and the code gives the error "Missing auth credentials".

4

1 Answer

As suggested from above comments, added the missing space after "Basic" in the header, solved this problem.

let res = self
    .client
    .post(url)
    .header("Content-type", "application/x-www-form-urlencoded")
    .header("Authorization", "Basic ".to_owned() + &secret)
    .send()
    .await?;
2
Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.