0

I am trying to use spotify's Oauth2 but when I try to get the token the response says "411 POST requests require a Content-length header. That’s all we know." but I have added the header to my post request.

#[get("/?<code>")]
fn spotify_oauth(code: String) -> String {
    println!("{}", code);
    let client = reqwest::Client::new();
    let url = format!("https://accounts.spotify.com/api/token/?grant_type=authorization_code&code={}&redirect_uri=http://localhost:8000", code);
    let res = client.post(&url)
        .header(reqwest::header::Authorization("Basic MYAUTH=".to_owned()))
        .header(reqwest::header::ContentType(reqwest::mime::APPLICATION_WWW_FORM_URLENCODED))
        .header(reqwest::header::ContentLength(0))
        .send().unwrap().text().unwrap();

    res
}

Thank you.

Gus
  • 151
  • 2
  • 15
  • 1
    Shouldn't you send credentials (ie username and password) somehow? My guess would be that instead of adding the `ContentType` and `ContentLength` headers manually, you should call [`form`](https://docs.rs/reqwest/0.10.1/reqwest/#forms) with the right data and let reqwest handle the headers. – Jmb Feb 20 '20 at 07:58
  • That seems correct, the Spotify documentation says: "The **body** of this POST request must contain the following parameters". This means they probably have a check for `int(headers.get('ContentType') or 0) != 0`, something along those lines because POST-ing to the endpoint with an empty body makes no sense. – Masklinn Feb 20 '20 at 09:35

0 Answers0