Example: http_with_auth
Example
To run this example:
- Clone the examples repository:
git clone git@github.com:alloy-rs/examples.git
- Run:
cargo run --example http_with_auth
//! Example of using the reqwest HTTP client with an `Authorization` header to get the latest block
//! number.
use alloy::{
providers::{Provider, ProviderBuilder},
rpc::client::RpcClient,
transports::http::{
reqwest::{
header::{HeaderMap, HeaderValue, AUTHORIZATION},
Client,
},
Http,
},
};
use eyre::Result;
#[tokio::main]
async fn main() -> Result<()> {
// Set the Authorization header.
let mut headers = HeaderMap::new();
headers.insert(AUTHORIZATION, HeaderValue::from_static("deadbeef"));
// Create the reqwest::Client with the AUTHORIZATION header.
let client_with_auth = Client::builder().default_headers(headers).build()?;
// Create the HTTP transport.
let rpc_url = "https://eth.merkle.io".parse()?;
let http = Http::with_client(client_with_auth, rpc_url);
let rpc_client = RpcClient::new(http, false);
// Create a provider with the HTTP transport.
let provider = ProviderBuilder::new().on_client(rpc_client);
// Get latest block number.
let latest_block = provider.get_block_number().await?;
println!("Latest block number: {latest_block}");
Ok(())
}
Find the source code on Github here.