Add a User-Agent Request Header to a reqwest Request in Rust

December 2025

This is how I'm adding User-Agent strings to requests for web resources in rust.

---
[dependencies]
anyhow = "1.0.100"
reqwest = {version = "0.12.24", features = ["blocking"] }
---

use anyhow::Result;
use reqwest::header::USER_AGENT;

fn main() -> Result<()> {
  let client = reqwest::blocking::Client::new();
  let res = client.get("https://www.example.com")
    .header(USER_AGENT, "MyUserAgent")
    .send()?;
  dbg!(res);
  Ok(())
}
Output:
[/Users/alan/.cargo/target/4b/0baba35be9214e/_active_nvim_run:15:3] res = Response {
    url: "https://www.example.com/",
    status: 200,
    headers: {
        "content-type": "text/html",
        "etag": "\"bc2473a18e003bdb249eba5ce893033f:1760028122.592274\"",
        "last-modified": "Thu, 09 Oct 2025 16:42:02 GMT",
        "cache-control": "max-age=86000",
        "date": "Wed, 03 Dec 2025 01:11:24 GMT",
        "content-length": "513",
        "connection": "keep-alive",
        "alt-svc": "h3=\":443\"; ma=93600",
    },
}
end of line

Endnotes

One reason I needed this is because api.weather.gov requires a User-Agent string.