tower-http icon indicating copy to clipboard operation
tower-http copied to clipboard

Add multiple headers in SetResponseHeaderLayer or similar

Open lf- opened this issue 7 months ago • 0 comments

  • [x] I have looked for existing issues (including closed) about this

Feature Request

I want to be able to set multiple headers with SetResponseHeaderLayer: needing one layer per header (plus me having issues returning impl Layer<S> relating to Send, Sync, etc), means that to set a bunch of default headers in a separate function than the one defining the service itself requires naming a type that is really long, made of a pile of tower::layer::util::Stack.

The use case for this is stuff like X-Content-Type-Options, X-Frame-Options, etc.

Motivation

There's a crate tower-default-headers which does this, but it's a third party crate and a pretty common use case.

Proposal

Some kind of SetResponseHeadersLayer that allows setting multiple headers, either using a Vec of them or a compile-time array or so.

Alternatives

Just use the third party crate; Just write your own middleware which involves unfun async shenanigans.

lf- avatar May 20 '25 22:05 lf-