Adding multiple custom headers in Reqwest

I'm trying to use reqwest to send an API call and need to set some custom headers but unsure how to do so. Would anyone have some advice on how I adjust my function below or what I need to do to add the 'map' of headers (or add several custom headers)?

The function currently looks like this:

fn get_token(clid: String, t: String, sign: String) {
    println!("clid: {}, t: {}, sign: {}", clid, t, sign);
    let mut map = HashMap::new();
    map.insert("Accept-Encoding", "gzip, deflate");
    map.insert("Connection", "keep-alive");
    map.insert("host", "openapi.tuyaeu.com");
    map.insert("client_id", clid);
    map.insert("sign", sign);
    map.insert("sign_method", "HMAC-SHA256");
    map.insert("t", t);
    map.insert("method", "GET");
    map.insert("url", "https://callurl");
    let client = reqwest::Client::new();
    let call = client
        .get("https://callurl")
        .header()  //how do I get my 'map' of headers into here??
        .send().expect("couldnt send request")
        .text().expect("couldnt read response text")
    println!("{:#?}", call);
}

The reqwest documentation suggests using:

use hyper::header::Headers;

But that feature doesn't appear to be there anymore so I'm a bit stuck.
Thanks all.

The one in hyper was likely a reexport from http, so import it from there. You will have to figure out which version of http corresponds to your reqwest version. You can do this by looking at the correct reqwest version on docs.rs, because dependencies are listed in a dropdown on the top of the page.

Where did you find it in the documentation? Just so we can have it fixed.

thanks Alice.
the documentation I found is for a much older version of Reqwest (https://docs.rs/reqwest/0.6.1/reqwest/header/index.html) but it includes a really simple example of how to define custom values.
I'm trying to look for the equivalent feature (simple example) in the newer versions.

Sorry I'm a newb at Rust but is the syntax I should be using once I import 'http' crate something like the following:

let mut map = HeaderMap::new();

map.insert(MY_CUSTOM_HEADER1, "my custom value 1".parse().unwrap());
map.insert(MY_CUSTOM_HEADER2, "my custom value 2".parse().unwrap());

What is the syntax that would then go into my '.header()' line above? Is it something like this?:

.header(&map)  //how do I get my 'map' of headers into here??

From the RequestBuilder::headers docs for reqwest v0.9

use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT, CONTENT_TYPE};

fn construct_headers() -> HeaderMap {
    let mut headers = HeaderMap::new();
    headers.insert(USER_AGENT, HeaderValue::from_static("reqwest"));
    headers.insert(CONTENT_TYPE, HeaderValue::from_static("image/png"));
    headers
}

let file = fs::File::open("much_beauty.png")?;
let client = reqwest::Client::new();
let res = client.post("http://httpbin.org/post")
    .headers(construct_headers())
    .body(file)
    .send()?;

thanks Nick, but how do I go about adding a custom header not one that is already defined like USER_AGENT or CONTENT_TYPE?

i.e. My request has a header value and key:
"user_id": "my user id" but there is no USER_ID constant under reqwest::header'.

I'm trying to figure out what the 'use' statement would look like for me as I've multiple headers in the request to send that are custom entries like above.. and also what would be the syntax for the 'header.insert(...)' line?

With HeaderName::from_static

use reqwest::header::{HeaderMap, HeaderName, HeaderValue, USER_AGENT, CONTENT_TYPE};

fn construct_headers() -> HeaderMap {
    let mut headers = HeaderMap::new();
    headers.insert(USER_AGENT, HeaderValue::from_static("reqwest"));
    headers.insert(CONTENT_TYPE, HeaderValue::from_static("image/png"));

    headers.insert(HeaderName::from_static("client_id"), HeaderValue::from_static("image/png"));
    headers
}
1 Like

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