Rust reqwest::Error when making a POST request

i get the following error while creating this POST request. Im a newbie into Rust.

Instead of serde_json::Value I have even tried HashMap<String, String> still the same issue.
if you could tell me if my headers are wrong or how do I trace if its actually a network reqwest issue or not ?

This is the actual response Im expecting. Session I have tried to replace serde_json::Value to Session still no effect the error persists.

#[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
}

I'm using Popos 20.04 and 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
    }

And this would just give me the following error:
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"

Thank you.

It looks like the server is not returning valid JSON. Try getting the response as text and printing it to see what it's actually returning.

1 Like

@Heliozoa Thank you <3 for suggestions.

I extracted the response. this is what I got.

       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"

Below thing fixed it
headers.insert(CONTENT_LENGTH, HeaderValue::from_static("0"));

I wasted 2 days on this :frowning:

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.