pub struct RequestIdMiddleware { /* private fields */ }Expand description
Middleware that adds unique request IDs to requests and responses.
This middleware:
- Checks for an existing X-Request-ID header from the client
- If present and valid, uses it; otherwise generates a new ID
- Stores the ID in request extensions for handlers to access
- Adds the ID to response headers
§Example
ⓘ
use fastapi_core::middleware::RequestIdMiddleware;
let mut stack = MiddlewareStack::new();
stack.push(RequestIdMiddleware::new());
// In your handler:
async fn handler(ctx: &RequestContext, req: &Request) -> Response {
if let Some(request_id) = req.get_extension::<RequestId>() {
println!("Request ID: {}", request_id);
}
Response::ok()
}Implementations§
Source§impl RequestIdMiddleware
impl RequestIdMiddleware
Sourcepub fn with_config(config: RequestIdConfig) -> Self
pub fn with_config(config: RequestIdConfig) -> Self
Creates a new request ID middleware with the given configuration.
Trait Implementations§
Source§impl Clone for RequestIdMiddleware
impl Clone for RequestIdMiddleware
Source§fn clone(&self) -> RequestIdMiddleware
fn clone(&self) -> RequestIdMiddleware
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for RequestIdMiddleware
impl Debug for RequestIdMiddleware
Source§impl Default for RequestIdMiddleware
impl Default for RequestIdMiddleware
Source§impl Middleware for RequestIdMiddleware
impl Middleware for RequestIdMiddleware
Source§fn before<'a>(
&'a self,
_ctx: &'a RequestContext,
req: &'a mut Request,
) -> BoxFuture<'a, ControlFlow>
fn before<'a>( &'a self, _ctx: &'a RequestContext, req: &'a mut Request, ) -> BoxFuture<'a, ControlFlow>
Called before the handler executes. Read more
Auto Trait Implementations§
impl Freeze for RequestIdMiddleware
impl RefUnwindSafe for RequestIdMiddleware
impl Send for RequestIdMiddleware
impl Sync for RequestIdMiddleware
impl Unpin for RequestIdMiddleware
impl UnwindSafe for RequestIdMiddleware
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).