0

I am having an issue passing data to Reqwest. I am using serde_urlencoded to create a string for a POST request. I receive the error below. I have confirmed that the data I use for request is correct with curl. It leads me to believe my body or headers are incorrect.

May someone please help me understand what is going wrong here with the request?

Error: {\"error\":\"unsupported_grant_type\",\"error_description\":\"grant_type parameter is missing\"}

Vec<u8> body and HeaderMap == Hashmap<String, Vec<String>>

    // application/x-www-form-urlencoded Mime type body
    let body = serde_urlencoded::to_string([
        ("grant_type", "authorization_code"),
        ("code", "authcode"),
        ("redirect_uri", "redirect.com"),
        ("code_verifier", "workingVerifier" ),
        ("client_id", "SomeID"),
        ("client_secret", "SomeSecret"),
    ])?;

    let body = serde_json::to_vec(&body)?;

 let mut headers: HeaderMap = HeaderMap::new();
    headers.insert(
        "Authorization".to_string(),
        vec![format!("Basic {}", generate_auth_string(conf).await?)],
    );
    headers.insert(
        "Content-Type".to_string(),
        vec!["application/x-www-form-urlencoded".to_string()],
    );

Reqwest

 let response = reqwest::Client::new()
            .request(method, &req.url)
            .headers(headers)
            .body(body)
            .send()
            .await
Stephen Andary
  • 113
  • 1
  • 10
  • 1
    Why are you serializing your urlencoded data as JSON? – kmdreko Mar 22 '23 at 18:59
  • I was using serde_json to convert to vec, but I think that may be the issue! – Stephen Andary Mar 22 '23 at 19:06
  • You can pass a `String` directly to `.body()`. I think the effect of re-serializing it as JSON would be to add quotes around it, which would be wrong. – kmdreko Mar 22 '23 at 19:09
  • I can't because the struct is not my own. It was not the issue. I tried as_bytes().to_vec(), no bueno. I will try again for good measure though – Stephen Andary Mar 22 '23 at 19:11
  • @kmdreko THAT WAS IT, thank you! Oh man I need to be careful with serde's. This happened to me with the serde_urlencoded crate the other day. – Stephen Andary Mar 22 '23 at 19:24

1 Answers1

0

Per @kmdreko I needed to change my body to not use serde_json.

let body = body.as_bytes().to_vec();

Stephen Andary
  • 113
  • 1
  • 10