5

创建此 POST 请求时出现以下错误。我是 RUST 的新手。

而不是serde_json::Value我什至尝试HashMap<String, String>过同样的问题。如果你能告诉我我的标题是否错误,或者我如何追踪它是否真的是网络 reqwest 问题?

这是我期待的实际反应。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"

谢谢你。

4

1 回答 1

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"

下面的东西修复了它,在 fnConstruct_headers 下添加 headers.insert(CONTENT_LENGTH, HeaderValue::from_static("0"));

于 2020-08-27T15:29:11.503 回答