reqwest :: Error {种类:解码,来源:Error“期望值”,第1行,第1列,}

问题描述

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

我什至没有尝试serde_json::Value,而是尝试HashMap<String,String>。 如果您可以告诉我我的标题是否错误,或者如何跟踪它是否确实是网络需求问题?

这是我期望的实际响应。 Session我尝试将 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("expected value",line: 1,column: 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"

谢谢。

解决方法

我提取了答复。这就是我得到的。

       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 Construct_headers下 headers.insert(CONTENT_LENGTH,HeaderValue::from_static("0"));

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...