#middleware #header #request-id #router #extract-request-id

request_id_middleware

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

4 releases (2 breaking)

Uses new Rust 2024

new 0.3.1 Apr 23, 2025
0.3.0 Apr 21, 2025
0.2.0 Apr 21, 2025
0.1.0 Apr 21, 2025

#1063 in HTTP server

Download history 368/week @ 2025-04-20

369 downloads per month

Custom license

11KB
131 lines

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.

Dependencies

~6–12MB
~127K SLoC