reqwest::Error { kind: Decode, source: Error("expected value", line: 1, column: 1) }' reqwest::Error { 类型: 解码, 来源: 错误("期望值", 行: 1, 列: 1) }'

11

我在创建POST请求时遇到了以下错误。我是RUST的新手。

我甚至尝试使用HashMap<String, String>而不是serde_json::Value,但仍然出现相同的问题。如果您能告诉我我的标头是否有误或如何追踪它是否实际上是网络reqwest问题,那会很好。

这是我期望的实际响应。我已经尝试将serde_json::Value替换为Session,但仍然没有效果,错误仍然存在。

#[derive(Debug, Deserialize, Serialize)]
pub struct Session {
    pub platform_type: String,
    pub ticket: String,
    pub profile_id: String,
    pub user_id: String,
    pub name_on_platform: String,
    pub expiration: String, //2020-08-26T16:46:59.4772040Z
}

我正在使用 Popos 20.04 和 Rust 1.45.2

    pub async fn login(&self) -> Result<serde_json::Value, reqwest::Error> {
        let response = self.client
            .post(request_url)
            .headers(self.construct_headers())
            .basic_auth(self.email.clone(), Some(self.password.clone()))
            .send().await?
            .json::<serde_json::Value>().await?;

        Ok(response)
    }

    fn construct_headers(&self) -> HeaderMap {
        let mut headers = HeaderMap::new();
        headers.insert("ubi-appid", HeaderValue::from_str(self.ubi_config.appid.as_str()).unwrap());
        headers.insert(USER_AGENT, HeaderValue::from_static("reqwest"));
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
        headers
    }

这会导致以下错误信息:

reqwest::Error { kind: Decode, source: Error("预期值", 行: 1, 列: 1) }
[dependencies]
serenity = "0.9.0-rc.0"
reqwest = "0.10.8"
tokio = { version = "0.2.22", features = ["full"] }
serde = {version = "1.0.114", features = ["derive"]}
serde_json = "1.0.57"
dotenv = "0.15.0"
config = "0.10.1"


感谢您的选择。
1个回答

4

我提取了响应,这就是我得到的。

       let response = self.client
            .post(request_url)
            .headers(self.construct_headers())
            .basic_auth(self.email.clone(), Some(self.password.clone()))
            .send().await?;

        println!("Status: {}", response.status());
        println!("Status: {:#?}", response.text().await?);

Status: 411 Length Required
Status: "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\"http://www.w3.org/TR/html4/strict.dtd\">\r\n<HTML><HEAD><TITLE>Length Required</TITLE>\r\n<META HTTP-EQUIV=\"Content-Type\" Content=\"text/html; charset=us-ascii\"></HEAD>\r\n<BODY><h2>Length Required</h2>\r\n<hr><p>HTTP Error 411. The request must be chunked or have a content length.</p>\r\n</BODY></HTML>\r\n"

以下内容修复了它,在fn的构造头部下添加: headers.insert(CONTENT_LENGTH, HeaderValue :: from_static(“0”));


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接