Skip to main content

HttpMiddleware

Trait HttpMiddleware 

Source
pub trait HttpMiddleware: WasmCompatSend + WasmCompatSync {
    // Provided methods
    fn before_request_headers<'a>(
        &'a self,
        _method: &'a Method,
        _uri: &'a Uri,
        _headers: &'a mut HeaderMap,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>> { ... }
    fn before_request_body<'a>(
        &'a self,
        _method: &'a Method,
        _uri: &'a Uri,
        _headers: &'a HeaderMap,
        body: Bytes,
    ) -> Pin<Box<dyn Future<Output = Result<Bytes, Error>> + Send + 'a>> { ... }
    fn after_response<'a>(
        &'a self,
        _method: &'a Method,
        _uri: &'a Uri,
        _status: StatusCode,
        _headers: &'a HeaderMap,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>> { ... }
}
Expand description

Transport-boundary hooks applied by DynHttpClient around every request.

Methods default to no-ops. All header hooks run in attachment order, then all body hooks run in that order with the final headers and preceding body replacements. Response hooks run in attachment order after the transport returns a response, before streaming-body consumption. A transport that returns an error for an error status, as rig-reqwest does, skips them for that reply. Multipart requests skip body hooks.

Any hook error fails the request; request-side errors abort before sending. Response hooks cannot modify responses. Hooks run on the request future and must not block.

Provided Methods§

Source

fn before_request_headers<'a>( &'a self, _method: &'a Method, _uri: &'a Uri, _headers: &'a mut HeaderMap, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>

Mutate the outgoing request headers in place.

Runs before before_request_body. An error aborts the request before it is sent.

Source

fn before_request_body<'a>( &'a self, _method: &'a Method, _uri: &'a Uri, _headers: &'a HeaderMap, body: Bytes, ) -> Pin<Box<dyn Future<Output = Result<Bytes, Error>> + Send + 'a>>

Observe the serialized request body, returning the body to send.

Return body unchanged to pass through, or a replacement to rewrite the payload. headers reflects every middleware’s header mutations. Not invoked for multipart requests. An error aborts the request before it is sent.

Source

fn after_response<'a>( &'a self, _method: &'a Method, _uri: &'a Uri, _status: StatusCode, _headers: &'a HeaderMap, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>

Observe the response status and headers as soon as they arrive. Not invoked when the transport fails the request, which includes a non-success status on a transport that reports one as an error.

For streaming responses this runs before any of the body stream is consumed. Observe-only: the response cannot be modified, but returning an error fails the request with that error.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§