Closed Israel-Laguan closed 1 year ago
Good question(s)!
set
, have you imported the Context
trait like the error is suggesting? It should be a public trait on any context, including BasicHyperContext
.remove
method also exists for headers (see here.) This should let you remove a header, update it to what you want, then add it again. One case that I can imagine this not supporting is if you needed the value previously set by another piece of middleware -- it might make sense to have remove
return an Option<String>
.For what you tried, request
is the incoming request, so grabbing any headers (or setting them!) would just affect the incoming side, not the outgoing side.
For what you tried,
request
is the incoming request, so grabbing any headers (or setting them!) would just affect the incoming side, not the outgoing side.
Please explain to me how to change the outgoing headers 🥺 🙏🏻
It's like you were originally asking -- set
and remove
alter the outgoing headers.
Context::set
: https://docs.rs/thruster/1.2.5/thruster/trait.Context.html#tymethod.setContext::remove
: https://docs.rs/thruster/1.2.5/thruster/trait.Context.html#tymethod.removeYou just need to remember to include the Context
trait via use thruster::Context;
I followed your suggestion and its working now:
Thanks for the help @trezm!
So to answer me, import use thruster::Context
allows modifying "outgoing" headers, no need to make "set" public.
In several places of the documentation and examples, it mentions the use of
context.set
. i.e.:https://github.com/thruster-rs/Thruster/blob/d80cf36ca3f8f40324dc5325a3c150aa5c833953/thruster/examples/custom_cors.rs#L39-L50
But when inspecting the types shows that
set
is not in the public API: https://github.com/thruster-rs/Thruster/blob/d80cf36ca3f8f40324dc5325a3c150aa5c833953/thruster/src/context/basic_context.rs#L191-L193(the same in all possible contexts)
How can I set the headers without it? Or can this be in the public API?
Use cases
My specific usecase is to implement a middleware like helmet.js or csrf. I need to Remove certain headers, modify others, and create new ones.
What I tried
Error output
My PC