rust - 如何通过使用 reqwest 传递 secret 来添加基本授权 header ?

标签 rust header reqwest

<分区>

我正在使用 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?;

我无法设置基本授权 header ,代码给出错误“缺少授权凭据”。

最佳答案

根据上面评论的建议,在标题中的“Basic”后面添加了缺失的空格,解决了这个问题。

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

关于rust - 如何通过使用 reqwest 传递 secret 来添加基本授权 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66534704/

相关文章:

c - 赋值使指针来自整数警告

rust - reqwest : Unable to deserialize JSON response + `match` issues 出现问题

vector - 是否有更有效的方法来调整表示为 Vec<T> 的二维网格的大小?

rust - 为什么cloned()允许这个函数编译

rust - 为什么返回 Result 的函数总是返回 Err?

loops - 引用的生命周期比相同范围内的值短?

c# - 单击事件的 GridView 列标题

c++ - 更改类中的变量值并将所有变量传递给新值以供进一步使用

node.js - 如何在 Rust 的 wasm_bindgen 函数中发出 HTTP 请求?

url - Rust URL 删除特定的 GET 参数