Skip to content

Instantly share code, notes, and snippets.

@hzbd
Last active August 17, 2025 07:54
Show Gist options
  • Save hzbd/084ccce261dd82b12d49052f05a634d9 to your computer and use it in GitHub Desktop.
Save hzbd/084ccce261dd82b12d49052f05a634d9 to your computer and use it in GitHub Desktop.
rust reqwest demo with headers
use reqwest::header::{HeaderMap, HeaderName, USER_AGENT, HeaderValue, CONTENT_TYPE};
use serde::{Deserialize, Serialize};
// use serde_json::json;
#[derive(Serialize, Deserialize, Debug)]
struct APIResponse {
http_via: String,
http_x_forwarded_for: String,
client_ip: String,
server: String,
at: String,
}
#[tokio::main]
async fn main() {
let url = format!("https://dadou.run/my");
fn construct_headers() -> HeaderMap {
let mut headers = HeaderMap::new();
headers.insert(USER_AGENT, HeaderValue::from_static("reqwest"));
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
headers.insert(HeaderName::from_static("x-api-key"), HeaderValue::from_static("123-123-123"));
headers
}
let client = reqwest::Client::new();
let response = client
.get(url)
.headers(construct_headers())
.send()
.await
.unwrap();
println!("Success! {:?}", response);
match response.status() {
reqwest::StatusCode::OK => {
// on success, parse our JSON to an APIResponse
match response.json::<APIResponse>().await {
Ok(parsed) => println!("Success! {:?}", parsed),
Err(_) => println!("Hm, the response didn't match the shape we expected."),
};
}
reqwest::StatusCode::UNAUTHORIZED => {
println!("Need to grab a new token");
}
other => {
panic!("Uh oh! Something unexpected happened: {:?}", other);
}
};
}
@asdzd029123asdssa231
Copy link

Helped a lot! Thankss

@wcarmon
Copy link

wcarmon commented Feb 24, 2024

agreed, thanks!

@InShade
Copy link

InShade commented Aug 16, 2025

Thanks. How to check client cookie and other headers that I have set? I see in Wireshark that they come from client to server. But in rust application: client_Headers: {}. link

@hzbd
Copy link
Author

hzbd commented Aug 17, 2025

@InShade

The reason client_Headers: {} prints an empty HeaderMap is due to how reqwest handles default headers and request building:

  1. client.default_headers(headers): This line sets the provided headers as default headers for the reqwest::Client instance. These headers are associated with the client itself, not immediately with individual Request objects you build.
  2. let r: reqwest::Request = client.get("http://dadou.run/my").build()?;: When you call build() on a request builder, you are creating a reqwest::Request object (r). At this point, this r object only contains headers that were explicitly added during its construction (e.g., if you used .header("Key", "Value") in the chain). The Client's default_headers have not yet been merged into this r object.
  3. Header Merging Timing: The reqwest library internally applies the Client's default_headers to the Request object only when client.execute(r).await? (or client.send(r).await?) is called. This merging process happens just before the request is actually sent over the network.

Therefore, when you print r.headers(), you are inspecting the headers of the Request object before the Client's default headers have been merged into it. This is expected behavior, and it does not mean that your X-MY-HEADER and COOKIE headers will not be sent; they will be sent when client.execute(r) is called.


println!("client_Headers: {:#?}", r.headers()); cannot be placed anywhere in your code to print the complete set of headers, including the Client's default headers, before client.execute() is called.

Your requirement (to print the complete request headers, including Client's default headers, before sending) cannot be directly achieved using reqwest's public API. You must rely on external tools (like network packet sniffers/proxies) or server-side logging/echoing to verify that these headers are indeed sent.

@InShade
Copy link

InShade commented Aug 17, 2025

@hzbd
Thank you very much, you helped me.
Similar topics
My code correction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment