request_id_middleware

Crates.iorequest_id_middleware
lib.rsrequest_id_middleware
version0.3.1
created_at2025-04-21 14:22:14.964871+00
updated_at2025-04-23 13:13:34.542613+00
descriptionCustom extractor for Rust Axum to extract the request id from an HTTP header X-Request-Id.
homepage
repositoryhttps://github.com/nebetoxyz/rust-request-id-middleware--lib
max_upload_size
id1642700
size30,385
François GRUCHALA (fgruchala)

documentation

README

Rust Axum Middleware - Extract Request ID from Header

Custom extractor for Rust Axum to extract the request id from an HTTP header X-Request-Id. Works ONLY with Rust Axum.

Usage

use axum::{routing::get, Router};
use request_id_middleware::ExtractRequestId;

async fn handler(ExtractRequestId(request_id): ExtractRequestId) {
    println!("Request Id: {:?}", request_id);
}

let app = Router::<()>::new().route("/foo", get(handler));

The extracted value is :

  • trim to clean extra spaces, before and after ;
  • lowercase to standardize and make it more resilient to implementation errors.

If the extracted value is not a valid UUID v7, it returns a 400 Bad Request with one of these two messages :

  • Invalid X-Request-Id : Not a valid UUID : it's a parsing error ;
  • Invalid X-Request-Id : Not an UUID v7 : it's a version error.

Samples

Extract version if the header is explicitly set

curl -H "X-Request-Id: 0196583c-4d2a-7087-9beb-6214d18ec924" http://api.nebeto.xyz/foo
curl -H "x-request-id: 0196583c-4d2a-7087-9beb-6214d18ec924" http://api.nebeto.xyz/foo
curl -H "X-ReQuest-ID: 0196583c-4d2a-7087-9beb-6214d18ec924" http://api.nebeto.xyz/foo

Will give for all 0196583c-4d2a-7087-9beb-6214d18ec924.

Extract version if the header is missing

curl http://api.nebeto.xyz/foo

Will give by default a newly generated UUID v7 e.g. 0196583c-4d2a-7087-9beb-6214d18ec924.

Contact

For any question or feature suggestion, you can take a look and open, if necessary, a new discussion.

For any bug, you can take a look to our active issues and open, if necessary, a new issue.

Commit count: 9

cargo fmt