How to solve this error when calling API with headers?

I'm using reqwest dependency to call an API. But, when I used normal get API call, it gives me the successful response. But, when I use post API call with headers, it keep telling me there are errors with .header.

Here is the error.

this function takes 1 parameter but 2 parameters were supplied
expected 1 parameterrustc(E0061)

Here is the code.

extern crate reqwest;
use reqwest::header::Authorization;
use reqwest::header::ContentType;

pub fn authenticate() -> String {

let res = reqwest::get("http://api.github.com/users")
.expect("Couldnt")
.text().expect("no res");

let client = reqwest::Client::new();
let resz = client.post("https://api.test.com/auth/token")
    .header(Authorization, "Basic abgtyhjmkiuyhjnmjhyuik==")
    .header(ContentType, "application/x-www-form-urlencoded")
    .body("grant_type=client_credentials")
    .send();

return res;
}

RustH1

What am I doing wrong ?? How can I fix this ??

You're probably using an older version of reqwest. In 0.8, the header method accepted 1 argument (see docs), but that API was changed in later versions.

1 Like

@Riateche - I used version 0.8.8. And I just tried like in the doc. And it gives me this below error.

mismatched types
expected struct std::string::String, found enum std::result::Result
note: expected struct std::string::String
found enum std::result::Result<search::routes::reqwest::Response, search::routes::reqwest::Error>rustc(E0308)
common.rs(4, 26): expected std::string::String because of return type
common.rs(30, 8): expected struct std::string::String, found enum std::result::Result

Here is new code.

extern crate reqwest;
use hyper::header::{Headers, Authorization, Basic, ContentType};

pub fn authenticate() -> String {

fn construct_headers() -> Headers {
    let mut headers = Headers::new();
    headers.set(
        Authorization(
            Basic {
                username: "AR:ABCRTY:1567:HI".to_owned(),
                password: Some("AHTYRHY".to_owned())
            }
        )
     );
    headers.set(ContentType::form_url_encoded());
    headers
}

let client = reqwest::Client::new();
let resz = client.post("https://api.test.com/auth/token")
    .headers(construct_headers())
    .body("grant_type=client_credentials")
    .send();

return resz;
}

How can I solve this ??

I'm guessing, here, because is not clear where the error is in your code.

I think you have to change the return type to be a Result, not a String.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.